1

I'm using ImageMagick to convert PDF files to PNGs. (Lots of text, so I'd rather use PNG over JPEG.) I'm doing this on OS X, 10.8.2.

I tried using Ghostscript, then ImageMagick, as was done here and I got a gray background. I shortened it to one line, since ImageMagick can work with PDFs and tried this:

 convert -background transparent -transparent white \
          Background-Page01.pdf TestClearX1.png

I've tried that with PNG files and JPEG files. (And with and without -transparent white as well.)

If it's a JPEG, I can get a white background (which is probably clear, but in my viewer, I can't tell), but with a PNG, I always get a dark background. I thought about, as a test, trying to generate a BMP, then converting that to PNG, but it won't generate BMP files.

How can I get a transparent background for a PNG file?

And if that's not possible, since JPEG files are not good for text, is there a better alternative?

Community
  • 1
  • 1
Tango
  • 649
  • 1
  • 12
  • 29
  • Does the PDF itself have a transparent background? Also, are these multipage PDFs? – PinnyM Dec 27 '12 at 22:15
  • Single page PDFs. They appear to be transparent backgrounds. The ones I'm using for testing are mostly text (I have one with images, which I'll use after I get text only working). I generated them from LibreOffice. I created a normal text document - no colors used in anything (black letters on the normal white background). I'm using a 4 page document and a 94 page document. I have generated 1 page PDFs of all 4 pages of the first document and a few pages of the second one. – Tango Dec 27 '12 at 22:29
  • Just tested this; assuming the PDFs have transparent backgrounds `convert foo.pdf bar.png` should work just fine to produce transparent PNGs. Also tested `convert -transparent white foo.pdf bar.png` (with white background in PDF) and got correct results. If you're still having issues make sure your ImageMagick install is up to date and you don't have wonky PDFs. – Matt Dec 27 '12 at 22:35
  • @Matt: Any idea how I can verify the PDF is good, or has an appropriate background? They're generated by LibreOffice. – Tango Dec 27 '12 at 22:47
  • Er. Try re-saving the PDF. Open the PDF in *another program* and save it again (if you're on a Mac, Preview would work, if you have Adobe Acrobat Pro or Illustrator these will work anywhere). On another note, just to cover all the bases, you say the PNG is "dark"—please be sure that's not the background showing through :) Can you post an example PNG? – Matt Dec 27 '12 at 23:13
  • I loaded the PDF in Preview, then saved it. I converted the new file and it still came out dark (black ink, grey background). I also, from Preview, exported the PDF to a PNG and it saved with a clear or white background. I'll find a sample PGN to post - I can't use the material I'm testing for posting an image (copyright issues), but I'll get one up tonight. – Tango Dec 28 '12 at 00:54
  • @Matt: Thank you for the suggestions. That made me check out a few things I wouldn't have. I was positive I must have missed something obscure in ImageMagick. I went back to check through LibreOffice and to export a non-copyrighted page and it turns out I was using PDF/A. There's two ways to export a PDF in LibreOffice, and one way does not provide any warnings - naturally this is what I was using. The other way provides a warning that PDF/A cannot have transparent objects. So the problem was I was using PDF/A files that don't have transparent objects. – Tango Dec 28 '12 at 01:07
  • Good stuff, glad you've sorted it. – Matt Dec 28 '12 at 03:59
  • @Matt: Since you pointed me to the answer, if you want to write that up as an answer, I'll select it (since it was what was wrong). If not, I'll write it up - but want to let you get the credit if you're interested. – Tango Dec 28 '12 at 04:24

2 Answers2

2

This isn't an answer, but the format of the comment section doesn't allow sensible formatting, so I am offering it here instead.

You alluded to the fact that Preview in OS X doesn't show transparency properly, and that is correct. To get around that, I made the following script, which overlays the files you want to view onto a checkerboard pattern like Photoshop does. I save it as preview and then use chmod +x preview on it to make it executable. Here is the script:

#!/bin/bash
################################################################################
# preview
# Preview images using OSX "open" but overlay images on top of checkerboard 
# first so you can see transparency/transparent pixels, since Preview renders
# them grey :-(
################################################################################
for f in "$@"
do
   base=$(basename "$f")
   composite -compose Dst_Over -tile pattern:checkerboard "$f" "/tmp/$base"
   open -g "/tmp/$base"
done

That enables you to do:

/.preview file1.png file2.gif

and the -g option also means Preview doesn't steal focus too :-)

So, instead of looking like this:

enter image description here

it looks like this:

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
1

There's two ways to export a PDF in LibreOffice, and one way does not provide any warnings. The other way provides a warning that PDF/A cannot have transparent objects. The problem is you're using PDF/A files that don't support transparent objects—this results in your PNG always having a background colour.

Matt
  • 3,617
  • 2
  • 27
  • 39