Background transparence on PNG from PDF

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
webid

Background transparence on PNG from PDF

Post by webid »

Hi !

I try to convert a PDF file to a PNG with background transparence ...
After verification, the PDF file has a transparent background, but when I convert him on a PNG, i have a white background.

I try this command, but the background is still white :

Code: Select all

convert test.png -background none test.png
Can you propose some examples in order to keep the background white ?

Thank's.

ps : Sorry for my English, I'm French and I don't control this language completely ... :oops:
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Background transparence on PNG from PDF

Post by fmw42 »

convert test.png -fuzz XX% -transparent white test.png

XX is the fuzz percent for finding colors close to white and then making them transparent.

See -fuzz, -transparent at http://www.imagemagick.org/script/comma ... ptions.php
also for color specification see http://www.imagemagick.org/script/color.php
webid

Re: Background transparence on PNG from PDF

Post by webid »

Thank you. I use this command :

Code: Select all

convert test.pdf -fuzz 100% -transparent white test.png
My PNG is transparent :)
jon@techvalidate.com

Re: Background transparence on PNG from PDF

Post by jon@techvalidate.com »

Hi. I have a similar use case that perhaps someone more experienced with IM can help me with.

I also have a PDF with a transparent background that I need to convert to PNG with the original transparency intact. However, in my case, just setting the transparent color to white won't work -- there are drop shadows on elements in the PDF that I need to preserve, with transparency. So, just converting white pixels to transparent would give me white fringes on objects in the image, even if I use a bit of fuzz.

I have tried and failed to find the magic incantation to tell IM to treat transparent portions of the input PDF as transparent in the output PNG. Have tried various settings for -background and -alpha, to no avail.

Any help would be greatly appreciated.

Btw, I am using IM 6.3.9 on MacOS X 10.5.7.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Background transparence on PNG from PDF

Post by anthony »

Please note that using -fuzz XX -transparent is not a 'good' solution. All it does is replace any instance of pure or near pure white with transparency.

The problem with this is that images are rarely that simple. Usally text and drawings have edges that are a mix of the background and the object itself. that is they contain mixed colors to 'anti-alias' or make the displayed objects looks 'smoother' to the human eye.

As a result the image will have 'stair-cased edges', or 'white halo effects' around them.

However if you read the image in at a higher resolution, do the color replacement, and then re-sample the image back to the desired resolution (typically 72 or 90 dpi), then much of thos problems will be fixed by the resize operator 'cleaning up' the edges.

The better solution would be to somehow actually get IM to get the Ghostscript delegate to render the PDF file properly on transparency. Usually that is don by setting -colorspace RGBA before 'reading' the PDF page.

Try reading the PDF like this....

Code: Select all

convert  -background none -colorspace RGBA test.pdf  test.png
Let us know how it goes.

This does not always work, as the PDF may actually draw white, such as in a diagram on the page, but it should work most of the time.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply