1

I'm using im4java to convert image. The following program crashes with stack trace: Caused by: org.im4java.core.CommandException: convert: insufficient image data in file /tmp/magick-254901G7YJ9qaMQv5' @ error/jpeg.c/ReadJPEGImage/1154.

Looks like it's generating a temp file. Running the same command directly on ImageMagick command lines gives me correct results.

I'm running ImageMagick-6.9.10-61 and im4java 1.4.0

I wonder if anyone has any insights.

IMOperation op = new IMOperation();
op.addImage("-");
op.addImage("jpg:-");
op.quality(0.85);

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
    // image is of type MultipartFile
    Pipe pipeIn = new Pipe(image.getInputStream(), null);
    Pipe pipeOut = new Pipe(null, outputStream);

    convertCmd.setInputProvider(pipeIn);
    convertCmd.setOutputConsumer(pipeOut);
    convertCmd.run(op);

    return outputStream.toByteArray();
} catch (Exception e) {
           //
}
2
  • Did you try creating a temp file yourself? Just to check whether it's the pipes or something else. Commented Aug 21, 2019 at 11:33
  • @Thomas How should I do that? The temp file created every time is different. Can I configure a file to use? Commented Aug 22, 2019 at 1:56

1 Answer 1

1

I had a look at some older code where we've done something similar and I realized your problem is the use of 2 pipes where one has a null target and the other has a null source. That might cause the temp file to be used.

Instead of doing that you need to use one single pipe:

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
  Pipe pipe = new Pipe(image.getInputStream(), outputStream);

  convertCmd.setInputProvider( pipe );
  convertCmd.setOutputConsumer( pipe );

  return outputStream.toByteArray();
}

From the JavaDoc:

You can use the same Pipe-object for both ends of a process-pipeline.

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

3 Comments

No luck. Still got the same error. Actually the code was from im4java.sourceforge.net/docs/dev-guide.html, under the pipe section. So I think the code is correct. Maybe configuration issues?
@JFreebird that could be the case. Did you make sure that you're using the same IM installation, image and parameters as on the command line?
yes, exactly the same. I realized that I called the Info() before running convert, if I don't call Info() and use filesystem files as input/output (instead of pipe), then the issue is gone.

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.