0
votes

I am trying to remove transparency with ImageMagick. The following command works for most of the images but not all. It messes up the image in few cases. "-remove alpha" solves the problem but I dont know how to do that in Magick++ code. I am using following command which is not working with few images. Attached is one sample.

command: convert -flatten -background white a.png a-removeTransparency.jpg

original image

2

2 Answers

1
votes

An example in would look something like...

#include <Magick++.h>
#include <vector>

int main(int argc, const char * argv[]) {
    std::vector<Magick::Image> images;
    Magick::Image input, output;
    // Create example image
    input.size(Magick::Geometry(100, 100));
    input.read("GRADIENT:GREEN-TRANSPARENT");
    // Set background color
    input.backgroundColor(Magick::Color("RED"));
    // Add image to list
    images.push_back(input);
    // Perform flatten operation
    Magick::flattenImages(&output, images.begin(), images.end());
    // Safe to disk
    output.write("output.png");
    return 0;
}

Which would convert an image like...

input

to

output

The key thing to understand is that Magick++ has Standard Template Libary with many helper methods, and most methods expected a list(vector) of images to act on. If you can't find what you need on the Magick::Image class, then it's probably over on the STL.

0
votes

The proper command in Imagemagick is to put settings like -background before operators such as -flatten. And more importantly read the input first. This is even more important in IM 7. IM 6 is more forgiving. So try

convert a.png -background white -flatten removeTransparency.jpg