Dhinakar
Dhinakar

Reputation: 4151

Convert image using image magick via java command says error

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.

Upvotes: 1

Views: 5230

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347334

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...

Upvotes: 1

user784540
user784540

Reputation:

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.

Upvotes: 1

Related Questions