0

I am trying to use im4java to create a jpg file with yellow text centered on a black background.

Using Imagemagick, the following command line works:

convert -size 1400x1050 -stroke yellow -fill yellow -font Courier -pointsize 40 -gravity Center caption:"Sample Text\nLine 2" -fill black -opaque white ~/test.jpg

I tried the following in Java to replicate this:

        ConvertCmd cmd = new ConvertCmd();
        IMOperation op = new IMOperation();
        op.size(1400, 1050)
            .stroke("yellow")
            .fill("yellow")
            .font("Courier")
            .pointsize(40)
            .gravity("center")
            .caption("Sample Text\nLine 2")
            .fill("black")
            .opaque("white")
            .addImage("~/test.jpg");
        try {
            cmd.run(op);
        } catch(Exception e) {
            System.out.println(e);
        }

but executing this gives:

org.im4java.core.CommandException: org.im4java.core.CommandException: convert-im6.q16: no images defined `~/test.jpg' @ error/convert.c/ConvertImageCommand/3234.

If I change the command to:

op.version()

I get the same output as:

convert -version

All examples I have found online start with an existing image file, modify it, and then store the result, so I can't find help there.

I am on Linux Mint 22.2 with Imagemagick version 6.9.12 and im4java version 1.4.0.

What is incorrect in the im4java operation I am trying to do? Any help is appreciated.

1 Answer 1

1

The issue is that ~ (home directory) doesn't work in im4java, so use the full path like /home/yourusername/test.jpg. use full path for image.. specify the output file at the end with addImage("/home/yourusername/test.jpg").

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

7 Comments

Thank you for the fast reply. Unfortunately, changing .addImage("~/test.jpg") to .addImage(/home/jim/test.jpg") does not fix the problem. Now the exception complains "no images defined '/home/jim/test.jpg'"
More information: If /home/jim/test.jpg exists and I replace java op.size(1400,1050) with java op.addImage("/home/jim/text.jpg"); op.size(1400,1050) then the jpg file is edited and no error message is displayed. So there is no problem finding /home/jim/test.jpg, the problem is elsewhere.
Still more information. Even though the change in my previous comment removes the error, the image changes are not actually written to the output file.
It seems like you're not specifying the output image. Be sure to add both the input and output images. Also, check that you have the right permissions to write the output file. If it still doesn't work, try a simpler task like resizing to see if that works.
The Imagemagick (convert) command that I specified creates a jpg. I want to do the same thing using im4java, but trying to replicate the arguments directly does not work. I am not starting with a jpg file, modifying it, and trying to write it back. My comments refer to things that I tried that still do not work, including starting with a jpg and modifying it. There is something wrong with my im4java code that I cannot figure out.
I thought that the .addImage("/home/jim/test.jpg"); as the last op to be performed would write the file, but it does not. There does not seem to be a different im4java API that does what I want.
OK. Uday kiriti's answer was correct. There is another problem, specifically in im4java, or at least the version I am using, that creates the same error message, although it has nothing to do with the output file. Now all I have to do is determine if anyone is actually maintaining im4java. My initial investigation indicates the answer is NO!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.