1

I need to convert all the tif, jpeg, gif to jpg format. For this am using

        ProcessBuilder pb2 = new ProcessBuilder("convert.exe", "\"" +dest.toString()+ "\" ", "\" " + dest.getParent().toString().concat("/").concat(dest.getName().toString().substring(0, dest.getName().toString().lastIndexOf(".")).concat(".jpg"))+ "\" " );
        System.out.println("convert " + "\"" + dest.toString() + "\" " + "\" " + dest.getParent().toString().concat("/").concat(dest.getName().toString().substring(0, dest.getName().toString().lastIndexOf(".")).concat(".jpg")) + "\" " );
        pb2.redirectErrorStream(true);
        try {
            Process p2 = pb2.start();
            System.out.println("jpg done for " + dest.getName());
            new Thread(new InputConsumer(p2.getInputStream())).start();
            try {
                System.out.println("Exited with: " + p2.waitFor());
            } catch (InterruptedException ex) {
                Logger.getLogger(ImageFileCopy.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(ImageFileCopy.class.getName()).log(Level.SEVERE, null, ex);
        }

It is saying error "Invalid parameter - and Exited with: 4"

I also tried giving "C:\Program Files\ImageMagick-6.8.6-Q16\convert.exe". If i use full path system not showing error but wating for long time.

Any idea plz suggest.

2 Answers 2

1

If you're using ProcessBuilder there's no need to "quote" your parameters, this is the point of using ProcessBuilder, it will guarantee that each separate parameter is passed as an argument to the command

ProcessBuilder pb2 = new ProcessBuilder(
    "convert.exe",
    dest.toString(), 
    dest.getParent().toString().concat("/").concat(dest.getName().toString().substring(0, dest.getName().toString().lastIndexOf(".")).concat(".jpg")));

I also agree with Rafael's suggestion, a wrapper API will make life a LOT easier ...

[face palm]...Windows has it's own convert program which is accessible via the PATH environment variable.

Even when I used pb.directory and set the directory to the install location of ImageMagick, it still picked up the Windows/MS program...

Try adding the full path to convert.exe

ProcessBuilder pb2 = new ProcessBuilder(
    "C:\\Program Files\\ImageMagick-6.8.6-Q16\\convert.exconvert.exe",
    dest.toString(), 
    dest.getParent().toString().concat("/").concat(dest.getName().toString().substring(0, dest.getName().toString().lastIndexOf(".")).concat(".jpg")));

And thanks to this answer for pointing it out...

Sign up to request clarification or add additional context in comments.

4 Comments

if i run without "quote" also it is saying same error
similarly when i used ProcessBuilder pb2 = new ProcessBuilder(Config.imagemagickrootpath + "/" + "convert", "-density", "100", dest.toString(),"-background", "white", "-alpha", "remove", dest.toString(), dest.getParent().toString().concat("/").concat(dest.getName().toString().substring(0, dest.getName().toString().lastIndexOf(".")).concat(".jpg")) );. System is creating two images-o, images-1. May i know what is the reason?
Have you tried placing the "source" file reference as the first option (don't have imagemagic in front of me to check)
Duplication is due to "dest.toString()" called twice in the argument. fix it.
1

I recommend to use im4java to call ImageMagick from your java code.

It is opensource, has API to call many ImageMagick functions and is easy to use.

invokation of an ImageMagick resize-function (for example) looks like that:

// create command
ConvertCmd cmd = new ConvertCmd();

// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.addImage("myimage.jpg");
op.resize(800,600);
op.addImage("myimage_small.jpg");

// execute the operation
cmd.run(op);

Check this simple developer's guide for more information.

4 Comments

how to download the im4java jar
@Dhinakar here you go: im4java.sourceforge.net/#download and there's the link: sourceforge.net/projects/im4java/files just download source code and include it to your project.
Use ConvertCmd class as it is shown in my answer. Also in my answer there are links to full API and developer's guide, to let you customize this example as you wish.
In java code we can set the path by following way String myPath="C:\\Programs\\ImageMagick-6.3.9-Q16"; ProcessStarter.setGlobalSearchPath(myPath);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.