Preserving transparency when converting Vector to PNG

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
d7a7z7e7d

Preserving transparency when converting Vector to PNG

Post by d7a7z7e7d »

I'm trying to convert a vector (eps, ai or pdf specifically) to png while keeping the background transparent. It seems anytime I convert a vector to png, there will be a white background regardless. I've sort of got around this by using "-transparent #FFFFFF" to replace all white with transparency, however, if the original vector has white in it then it too gets replaced and that is not the desired result. Is there any way to do this vector to png conversion in imagemagick while keeping the transparency correct?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Preserving transparency when converting Vector to PNG

Post by anthony »

Using "-transparent #FFFFFF" is not a good solution, as it will likely leave a white halo around the lines and edges caused by the anti-aliasing aspects. Or if you increase the '-fuzz' factor to remove that you lose the anti-aliasing making your image very stiar case like, so that you may as well have used GIF instead of the nicer PNG to store your image.

Have you tried setting -background before reading in the image? In many cases the vector image does not define a background, so IM will default to the current -background setting, which defaults to 'white'.

If that fails and the vector image itself sets a white background, then what you can do will depend on what you plan to use the image for, and how complex the vector image is.

For example... In IM Examples, Text Handling, Postscript I read a postscript image and include examples of converting a white background postscript in a number of ways to simulate printing on a transparency, or onto some colored stock paper (using "-compose Multiply").

If the vector image being converted is simple enough, you could do some simple 'text file processing' to remove/change the background setting.
For example I do this for the Vector SVG format output of 'AutoTrace', in
Edging using a Raster to Vector Converter, to remap colors and stroke widths.

And finally if your really really MUST replace the white background with transparency, but would like to try and preserve the anti-aliasing pixels, then you can use a double 'fuzz' masking method that I have been developing in Masking with Anti-Aliased Edges. It is also related to work that was done by Dr Rick Mabry on Gradient Color Replacement.

Basically there are lots of ways to skin a cat, and what method you use depends on what part of that skin you want, and how messy you like the results!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
d7a7z7e7d

Re: Preserving transparency when converting Vector to PNG

Post by d7a7z7e7d »

The use of this vector to png conversion is for someone to upload several images using a Flash interface and then drag them around to make a design. The idea is that the images get uploaded in their original vector format via PHP, then converted to PNG with IM so Flash can read them back in. I need the PNGs to be as close to their original vector as possible, so the ideal solution is to just figure out a way to drop the white background in the vectors. I will only be accepting EPS, AI and PDF formats but its entirely acceptable if each of those formats have a unique way of doing the background removal.

So far I've tried just about everything I could find on the mailing lists and forums, this post was really my last resort as I'm a firm believer in the search button :) But in detail, I've tried the following with the parameters passed before and after the input filename:

"-background none"
"-channel RGBA"
"-colorspace RGB"
"-matte -fill none"

I've mixed and matched all of the above options without any luck. The major problem I seem to be running into with the "-transparent #FFFFFF" method is that I'll be removing all of the white from the image, which could be very bad considering the user can design on a black background and might have used white in their vector. And all the examples I've seen using the -fuzz method just seems to do a fill outside of the image, which might be nice to remove some of the jaggies, but it won't remove any remaining white inside of the vector that should of been transparent.

When I run "identify -verbose" on my test EPS, AI or PDF, it would appear that it has the background set to white:

Background color: white
Border color: cmyk(223,223,223,0)
Matte color: grey74
Transparent color: black

The ideal solution I think is to just force that to be transparent. How? I have no idea :)

If you have any other suggestions now that you have a better understanding of my usage, I'd really appreciate your thoughts and sincerely thank you for your time.

P.S. Here is the artwork I'm using to test with:

http://www.2shared.com/file/3789731/13186499/Image.html (EPS)
http://www.2shared.com/file/3789740/2b5ec2c8/Image.html (AI)
http://www.2shared.com/file/3789745/5b343647/Image.html (PDF)
d7a7z7e7d

Re: Preserving transparency when converting Vector to PNG

Post by d7a7z7e7d »

UPDATE:

I was able to get the EPS transparency working with both PostScript 2 and 3!

The command to do this was: "convert -channel RGBA -colorspace RGB Image.eps Image.png"

I immediately tried the same command on my test AI and PDF files but it didn't work and the white background was still there. So now I just need to figure out a similar command for the AI and PDF files, OR, perhaps there is a way to convert the AI and PDF to EPS before I pass it to IM? It sounds like an added layer of complexity and perhaps another performance hit, but in all reality I don't care how it gets done just so long as it works.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Preserving transparency when converting Vector to PNG

Post by magick »

We use Ghostscript to render EPS / PDF files. Ghostscript supports an alpha channel for the PNG format but PNG only permits one page per image so we can only utilize it for EPS (since EPS by definition is one page). Try using Ghostscript directly from the command line for your problem.
d7a7z7e7d

Re: Preserving transparency when converting Vector to PNG

Post by d7a7z7e7d »

I received a response from Ross Presser on the ImageMagick mailing-list that suggested using the following Ghostscript command:

gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r72 -dEPSCrop -sOutputFile=output.png input.eps

This works perfectly for EPS! When using AI it does the transparency correctly but the canvas is very large and my image shows up in the bottom left corner. Is there a command to trim the excess? It doesn't appear to work for PDF at all because it just errors out. Does Ghostscript even support PDF? I figure now I might be able to find some PDF to EPS converters and be all set (any suggestions?)...

Thanks again.
d7a7z7e7d

Re: Preserving transparency when converting Vector to PNG

Post by d7a7z7e7d »

Ok so everything is finally working. Thanks to everyone for their help!

The solution ended up being:

gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r72 -dEPSCrop -sOutputFile=Image.png Image.eps

I've tested this to work with EPS, AI and PDF files as input without any modification of the parameters. Earlier I said it wasn't working with PDF, but there was just a mistake in syntax and it should work just fine if you use the above command.

Also, in order to solve a problem where I had excess canvas, I was able to use IM to trim the excess using the following command after I passed it through GS:

convert -trim Image.png Image.png

I now am able to convert various vectors to PNG and maintain the transparency correctly!

Thanks again!
jstango

Re: Preserving transparency when converting Vector to PNG

Post by jstango »

Hi All-

I've tried to use this method to create a transparent PNG from EPS files I currently have. Everything seems to work ok but after the conversion I get a PNG is very washed out and doesn't look the same as the original. (See example below)

Here are the commands I've tried.

convert -density 300 -colorspace rgb buil002.eps buil002.png
gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r72 -dEPSCrop -sOutputFile=buil002.png buil002.eps

EPS: http://www.2shared.com/file/4011424/682 ... il002.html
PNG: http://www.2shared.com/file/4011431/158 ... il002.html

Thanks,
Joe

PS If I use png16m as the device the image is rendered correctly but there is no transparency so this doesnt work for me.
jon@techvalidate.com

Re: Preserving transparency when converting Vector to PNG

Post by jon@techvalidate.com »

This is an old thread, but I'm having trouble with exactly the same issue as the original poster, so if any experienced IM user reads this, any help would be very much appreciated.

I also have PDFs with transparent backgrounds that I need to convert to PNGs with the original transparency intact. Also, in my case, just setting the transparent color to white with -transparent won't work -- there are drop shadows on elements in the PDF that I need to preserve with transparency.

The Ghostscript solution posted here gives me a PNG with a white background as well.

Any help would be greatly appreciated.

Btw, I am using IM 6.3.9 on MacOS X 10.5.7.
Post Reply