3

I need to convert from PNG to JPG.

However, iMagick adds a black background to it.

I saw this question which is for PHP, and tried to write the same for java like this:

// create the a jpg image
ConvertCmd cmd = new ConvertCmd();
// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.addImage(brandingURL);
op.format("JPEG");
op.composite();
op.background("white");
op.addImage(imageLocation);
//op.transparent();
// execute the operation
cmd.run(op);

But still, the image comes out with a black background.

What am I missing?

Community
  • 1
  • 1
Dejell
  • 13,947
  • 40
  • 146
  • 229
  • Just a guess: the conversion already replaces the transparent background of the PNG to black before setting the background to white. I assume the white background is then just overwritten with the black background. Maybe you should not convert the image but draw the PNG onto a white JPEG of the same size. – Thomas Apr 25 '12 at 12:01
  • I'm no ImageMagick expert (you might want to refer to its documentation - it might be the `draw` command) but with plain Java image io you'd create a `BufferedImage` and use the associated `Graphics` object to fill the background and draw the PNG on it. – Thomas Apr 25 '12 at 12:18
  • It's not going to help me. since the resolution will be low – Dejell Apr 29 '12 at 08:58
  • Have you tried to use `javax.imageio.ImageIO` instead? – Samuel Audet Apr 29 '12 at 09:03
  • 1
    Of course I tried, but the quality of the image is so poor that I need to use external libraries – Dejell Apr 29 '12 at 09:46

2 Answers2

2

I had to write the code like this:

 Info imageInfo = new Info(brandingURL, true);
 IMOperation op = new IMOperation();
 op.addImage(brandingURL);
 op.size(imageInfo.getImageWidth(), imageInfo.getImageHeight());
 op.addImage("xc:white", "c://write//test.jpeg");
 op.addImage("c://write//test.jpeg");
 CompositeCmd composite = new CompositeCmd();
 composite.run(op);
Dejell
  • 13,947
  • 40
  • 146
  • 229
0

The call to background should not be necessary. Accroding to the documentation, the default background is white, which suugests to me that maybe one of your pictures has a black background that overwrites/blocks the default (maybe the the one at brandingURL?).

Quote from the above linked documentation for ImageMagick:

-background color

Set the background color.

The color is specified using the format described under the -fill option. The default background color (if none is specified or found in the image) is white.

If you are using it precisely because one of the images does specify a (black) background, I suggest you move the background call to either before adding that picture or to the end of the operation (Not sure how ImageMagick operates wrt to this)

Community
  • 1
  • 1
Attila
  • 28,265
  • 3
  • 46
  • 55