Reading SVG image with transparent background

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
flavio
Posts: 2
Joined: 2014-12-02T07:45:32-07:00
Authentication code: 6789

Reading SVG image with transparent background

Post 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;
}

snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Reading SVG image with transparent background

Post 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.
snibgo's IM pages: im.snibgo.com
flavio
Posts: 2
Joined: 2014-12-02T07:45:32-07:00
Authentication code: 6789

Re: Reading SVG image with transparent background

Post 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 :)
Post Reply