Page 1 of 1

Reading SVG image with transparent background

Posted: 2014-12-02T08:12:29-07:00
by flavio
Hello,

this is my first post and I hope I can find the solution to my problem.

I have an SVG file with a transparent background and I try to compose it with another file (the background).
I use Image.read("file.svg"), then I compose it with the Image background and finally write to a jpeg file.

All is fine except the fact that the svg background becomes "white" when is loaded into the Image object and consequently, the composition results covered by the white background

My question is: how can I import the information about "transparent" background from the input image? The only function I know is Image.read(), but it seems to convert the transparent to white...

This is a my test case, where in the file '01_r.jpg' i cannot view the background.

Code: Select all


#include <Magick++.h>
#include <iostream>
#include <cstring>


using namespace std;
using namespace Magick;

int main(int argc, char **argv)
{
    InitializeMagick(*argv);

    Image svg;
    Image bkg;    

    try
    {
       
        // Read a file into image object 
        svg.read("r.svg");    
        
        //svg.transparent(Color("white")); <-- if I uncomment this, I can view the background, but this is not a solution, because the white is used, not just the background                

        bkg.read("r-01.bmp");        

        bkg.composite(svg, 0, 0, CompositeOperator::OverCompositeOp);
        bkg.quality(80);

        // Write the image to a file 
        bkg.write("01_r.jpg");
        
    }
    catch (Exception &error_)
    {
        cout << "Caught exception: " << error_.what() << endl;
        return 1;
    }
    return 0;
}


Re: Reading SVG image with transparent background

Posted: 2014-12-02T08:34:47-07:00
by snibgo
I don't know the answer for Magick++.

At the command level, the answer is to set "-background None" before reading the SVG.

Re: Reading SVG image with transparent background

Posted: 2014-12-02T08:51:15-07:00
by flavio
snibgo wrote:I don't know the answer for Magick++.

At the command level, the answer is to set "-background None" before reading the SVG.
You have found the solution!
I tried to set the backgroundColor BEFORE the READ of the SVG, using the string "None"... and it worked well.

The solved code:

Code: Select all


using namespace std;
using namespace Magick;

int main(int argc, char **argv)
{
    InitializeMagick(*argv);

    Image svg;
    Image bkg;    

    try
    {
       
        // Read a file into image object 
       
        
       svg.backgroundColor("None");
       svg.read("r.svg");  
                

        bkg.read("r-01.bmp");
        

        bkg.composite(svg, 0, 0, CompositeOperator::OverCompositeOp);
        bkg.quality(80);

        // Write the image to a file 
        bkg.write("01_r.jpg");
        
    }
    catch (Exception &error_)
    {
        cout << "Caught exception: " << error_.what() << endl;
        return 1;
    }
    return 0;
}
Thanks :)