Coverting PDFs: White background instead of transparency

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
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Coverting PDFs: White background instead of transparency

Post by whugemann »

When converting vector PDF pages to image formats that are aware of transparency, I often have the problem that the background becomes black, i.e. I would have to read black text on black background. Obviously, the canvas (i.e. the page) is considered to be fully transparent. (When converting to JPEG, the background is always white.) How could I achieve a white background instead?

Right now, I am trying to detoriate the quality of a vector PDF by rasterising it and rotating the pages by an arbitray angle:

Code: Select all

convert test.pdf  -distort SRT "%%[fx:int(rand()*10)-5]" -scene 1 test-%%02d.jpg
With this command line, the problem of dark background also affects the conversion to JPEG format.

If I convert directly to raster PDF

Code: Select all

convert -density 200 test.pdf  -distort SRT "%%[fx:int(rand()*2)-1]" -compress zip test_det.pdf
the conversion works. This direct conversion will however not work for PDFs with a larger page count, as it needs too much memory.
Wolfgang Hugemann
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Coverting PDFs: White background instead of transparency

Post by snibgo »

JPG doesn't have transparency, but will honour "-background", eg:

convert test.pdf -distort SRT "%%[fx:int(rand()*10)-5]" -scene 1 -background white test-%%02d.jpg

Does that answer the question? If not, can you provide a sample input?
snibgo's IM pages: im.snibgo.com
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Coverting PDFs: White background instead of transparency

Post by whugemann »

Does that answer the question? If not, can you provide a sample input?
No, it doesn't. I created a simple example, drawing a red circle and a blue quadrangle onto a blank page with Microsoft Word and then printing the page on a PDF printer (FreePDF aka FreePDFxp): http://www.unfallrekonstruktion.de/imag ... vector.pdf

If you convert this PDF to JPEG, the background is white. When converting to PNG, the background is transparent. And if you convert it to GIF, the background becomes black.

Convert seems to be unimpressed by the option -background, no matter whether you place it before or after the PDF.
Wolfgang Hugemann
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Coverting PDFs: White background instead of transparency

Post by snibgo »

For me (IM 6.7.9 on Windows 7), converting vector.pdf gives a transparent background for PNG or GIF, or white for JPG. If I want to convert transparent pixels to a specific colour such as green:

convert vector.pdf -background green -alpha remove out.jpg

This also works for out.png or out.jpg.

I use green as a test colour, because many image viewers will show "transparent" as white, which confuses me.

Does that help?
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Coverting PDFs: White background instead of transparency

Post by fmw42 »

whugemann wrote:Convert seems to be unimpressed by the option -background, no matter whether you place it before or after the PDF.
-background is a setting and does nothing by itself. It needs an operator to act upon it, such as -flatten, etc. see http://www.imagemagick.org/Usage/basics/#options

User, snibgo, used -alpha remove as the operator, which should work to change the transparency to some color.


Also I downloaded the vector.pdf and IM could not open it on my Mac OSX Snow Leopard IM 6.8.0.4 Q16, though my browser shows it fine.

identify -verbose vector.pdf
identify: unable to load module `/usr/local/lib/ImageMagick-6.8.0/modules-Q16/coders/pdf.la': file not found @ error/module.c/OpenModule/1278.
identify: no decode delegate for this image format `vector.pdf' @ error/constitute.c/ReadImage/550.

I am not sure what is the issue with this file?


Back to the transparency issue. This seems to work fine for me on IM 6.8.0.4 Q16

#create transparent pdf
convert logo: -transparent white logo.pdf

#result is a transparent png
convert logo_trans.pdf logo_trans.png

#result is a green background png
convert logo_trans.pdf -background green -flatten logo_green.png
User avatar
whugemann
Posts: 289
Joined: 2011-03-28T07:11:31-07:00
Authentication code: 8675308
Location: Münster, Germany 52°N,7.6°E

Re: Coverting PDFs: White background instead of transparency

Post by whugemann »

I gave it a check: I am working with ImageMagick 6.7.4-7 (January 2012) on Windows XP.

The option -alpha remove resulted in the error message: UnrecognizedAlphaChannelType `remove'.

It however helped to just choose the option -alpha off, which produced white background for any format. Strange enough this option however has to be placed before -distort SRT, i.e. on Windows:

Code: Select all

convert -density 200 %1[1] -alpha off -distort SRT "%%[fx:int(rand()*2)-1]" -scene 1 %~n1-%%02d.jpg
When placing it after the distortion, the background remains black.
I am not sure what is the issue with this file?
I can't tell. As I said, it was produced with the help of a simple PDF printer driver whichs relies on GhostScript. As PostScript is plain text, you can just open it in a text editor (what I have just done) and see that there is nothing special to it.
Wolfgang Hugemann
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Coverting PDFs: White background instead of transparency

Post by fmw42 »

The "-alpha Remove" method added to IMv6.7.5. See http://www.imagemagick.org/Usage/masking/#alpha_remove

I believe if you want -distort to process the alpha channel you need to specify -channel rgba (-alpha on) before -distort
Post Reply