1

When converting from PNG to JPG using the MagickWand API, how do I set the background to white for transparent pixels?

2 Answers2

3
if(current_wand && IsMagickWand(current_wand)){
    status=MagickReadImage(current_wand, "test.png");
    if (status == MagickFalse) {
        ThrowWandException(current_wand);
    }
    PixelWand *color = NewPixelWand();
    PixelSetColor(color, "white");
    MagickSetImageBackgroundColor(current_wand, color);
    MagickWand *newwand = MagickMergeImageLayers(current_wand, FlattenLayer);
    MagickWriteImage(newwand, "test.jpg");
    DestroyMagickWand(newwand);
}
Oldes
  • 937
  • 1
  • 9
  • 24
  • You're probably the only person I've seen provide Objective-C source code on SO for MagickWand(). Would you mind taking a crack at my question? http://stackoverflow.com/questions/7480812/iphone-imagemagick-library-converting-from-batch-file-script-to-objective-c – gotnull Sep 21 '11 at 02:01
  • I found that using `FlattenLayer` caused the resulting JPG to have different image dimensions (width or height) for some PNGs. Using `MergeLayer` instead fixes this for me. – foz Jan 24 '14 at 12:01
1

Use MagickMergeImageLayers