Error in im4java, Cannot run program "convert": error=2, No such file or directory

4.1k views Asked by At

I added gradle dependency for im4java 1.4.0. And trying to run the application in my linux system.

Here's the Code

public static void main(String[] args) throws Exception {
    // TODO Auto-generated method stub
    IMOperation op = new IMOperation();
    op.addImage();  
    op.resize(400, 500);// input
    op.addImage("png:-");                 // output: stdout
    BufferedImage image = ImageIO.read(new URL("http://pic.youmobile.org/imgcdn/google_icon_play_materiel.png")); 
    // set up command
    ConvertCmd convert = new ConvertCmd();
    Stream2BufferedImage s2b = new Stream2BufferedImage();
    convert.setOutputConsumer(s2b);


    // run command and extract BufferedImage from OutputConsumer
    convert.run(op,image);

    BufferedImage img = s2b.getImage();
    ImageIO.write(img, "png", new File("output.png"));

      }

But It was throwing the error

     Exception in thread "main" org.im4java.core.CommandException: java.io.IOException: Cannot run program "convert": error=2, No such file or directory
at org.im4java.core.ImageCommand.run(ImageCommand.java:219)
at com.quixey.media.service.converter.Test.main(Test.java:33)
     Caused by: java.io.IOException: Cannot run program "convert": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at org.im4java.process.ProcessStarter.startProcess(ProcessStarter.java:407)
at org.im4java.process.ProcessStarter.run(ProcessStarter.java:312)
at org.im4java.core.ImageCommand.run(ImageCommand.java:215)
... 1 more
     Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:248)
at java.lang.ProcessImpl.start(ProcessImpl.java:134)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 4 more

What was this error about ?How to fix it ?

2

There are 2 answers

0
Bruce Wilcox On

This code worked for me on my Mac.

import org.im4java.core.ConvertCmd;
import org.im4java.core.IM4JavaException;
import org.im4java.core.IMOperation;

public class PngTrimMacWithBrew {

    public static boolean trim(
        String originalFile,
        String outputFile
    ) {

        boolean success = false;

        // convert $originalFile -trim -verbose $trimmedFile

        // Create command
        ConvertCmd cmd = new ConvertCmd( );
        // Path to image magick
        String imPath = "/opt/homebrew/bin";
        cmd.setSearchPath( imPath );

        // Create the operation
        IMOperation op = new IMOperation( );
        op.addImage( originalFile );
        op.trim( );
        op.verbose( );
        op.addImage( outputFile );

        try {

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

            success = true;

        } catch ( IOException e ) {
            System.err.println( e.getMessage( ) );
        } catch ( InterruptedException e ) {
            System.err.println( e.getMessage( ) );
        } catch ( IM4JavaException e ) {
            System.err.println( "IM4JavaException " + e.getMessage( ) );
        }

        return success;

    }

3
Kishore On

It is trying to run imagemagick's convert command but that is not there in the PATH. im4java is just an interface to run imagemagic's commmands from a java program. You need to have imagemagick in the PATH.