PDF to PNG transparency conversion problem

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?".

PDF to PNG transparency conversion problem

Postby jfplante » 2011-08-15T14:28:30+00:00

Hi everyone,

I am having difficulties converting the seemingly simple pdf file linked below into a png, while respecting the transparency of the original pdf. The original image was much more complex but the problem seems to boil down to the presence of a raster placing with transparent background within the pdf (the file was created with illustrator, placing a "transparent" png and exporting as pdf). I do not control the origin of the pdf, so exporting from Adobe tools into png is not an option.
Here is the pdf that fails:
http://publicly-shared.s3.amazonaws.com/pdf_with_transparency.pdf

I have looked at various related posts without success (
http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=14546
http://stackoverflow.com/questions/653380/converting-a-pdf-to-png, and more...)

I also checked how the file looked like with identify, and it sees an alpha channel:
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit

Here are the versions I am using
Version: ImageMagick 6.7.1-1 2011-07-29
GPL Ghostscript 9.02

Any word of wisdom would be great,
JF
jfplante
 
Posts: 10
Joined: 2011-08-15T13:26:13+00:00

Re: PDF to PNG transparency conversion problem

Postby anthony » 2011-08-15T17:13:58+00:00

While a image may have an alpha channel, it does not actually mean the image uses the alpha channel!!!
Many images simply does not.

For Postscript and PDF conversions see
http://www.imagemagick.org/Usage/text/#postscript
and
http://www.imagemagick.org/Usage/formats/#ps

The problem is that by default transparency is not used, (using ppm image format from Ghostscript)
Unfortunately if transparency is requested then you can only get one page at a time. (using png image format from ghostscript)

You may be better of using ghostscript more directly. It is amazing just what circus tricks IM has to go though to deal with postscript and PDF itself. For an example see...
http://www.imagemagick.org/Usage/text/#ghostscript
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8731
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

Re: PDF to PNG transparency conversion problem

Postby jfplante » 2011-08-16T10:58:15+00:00

Thanks for the answer Anthony,

I should have mentioned first that opening the linked pdf file in os x preview or illustrator does show geometric figures over a transparent background.

I tried several of the tricks from those pages without success. Specifically, here a some of the commands I tried
- convert -channel RGBA pdf_with_transparency.pdf pdf_with_transparency.png
- changing delegates.xml to use pnmraw
Also, using ghostscript with pngalpha driver directly
- gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -sOutputFile=pdf_with_transparency.png pdf_with_transparency.pdf

Anything obvious I am missing?

Jf
jfplante
 
Posts: 10
Joined: 2011-08-15T13:26:13+00:00

Re: PDF to PNG transparency conversion problem

Postby anthony » 2011-08-18T17:34:16+00:00

pnmraw does not have alpha.

pngalpha does but it limited to one image

But that is the extent.

Best idea may be to try and work directly with ghostscript yourself, reading up on its output driver documentation.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8731
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

Re: PDF to PNG transparency conversion problem

Postby jfplante » 2011-08-23T17:54:42+00:00

Thanks Anthony,

I do have the problem with ghostscript as well using pngalpha driver. I actually found a post in the ghostscript forum post that seem very similar to the question I have, unfortunately it is not solved. (http://old.nabble.com/Problem-convertin ... #a31870245).

I will just add a manual override upon visual inspection until i get more time to dig into this.

Jf
jfplante
 
Posts: 10
Joined: 2011-08-15T13:26:13+00:00

Re: PDF to PNG transparency conversion problem

Postby fmw42 » 2011-08-23T18:17:01+00:00

If I understand this correctly, if you have a pdf image with only ONE page and transparency, then you can edit your delegates.xml file to change the device to pngalpha and that should work fine.


<delegate decode="ps:alpha" stealth="True" command="&quot;gsc&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pngalpha&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;"/>
User avatar
fmw42
 
Posts: 14888
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: PDF to PNG transparency conversion problem

Postby jfplante » 2011-08-29T14:57:14+00:00

Thanks Fred, sorry for the late reply.

Yes, you are correct: this is a one page pdf with transparency. I checked my ps:alpha delegate, and I already had pngalpha device set so no luck there.

Do you manage to get that file converted properly with your settings?
http://publicly-shared.s3.amazonaws.com/pdf_with_transparency.pdf

Best
Jf
jfplante
 
Posts: 10
Joined: 2011-08-15T13:26:13+00:00

Re: PDF to PNG transparency conversion problem

Postby fmw42 » 2011-08-29T15:56:43+00:00

Yes, it works fine for me. I am using IM 6.7.2.1 Q16 HDRI Mac OSX Tiger with Ghostscript 9.02.

Both the following work fine, keeping the transparency. (However, opening it in Mac PREVIEW on my old G4 Mac mini Tiger does not show the transparency for the pdf. But does show it for the png results.)

convert pdf_with_transparency.pdf pdf_with_transparency.png

and

convert pdf_with_transparency.pdf -depth 16 pdf_with_transparency2.png

However, I note your input image is 16-bits:

Code: Select all
Image: pdf_with_transparency.pdf
  Format: PDF (Portable Document Format)
  Class: DirectClass
  Geometry: 3226x2592+0+0
  Resolution: 72x72
  Print size: 44.8056x36
  Units: Undefined
  Type: PaletteMatte
  Endianess: Undefined
  Colorspace: RGB
  Depth: 16/8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
    alpha: 1-bit
  Channel statistics:
    Red:
      min: 54227 (0.827451)
      max: 65535 (1)
      mean: 65520.2 (0.999775)
      standard deviation: 407.437 (0.00621709)
      kurtosis: 760.555
      skewness: -27.6198
    Green:
      min: 7967 (0.121569)
      max: 65535 (1)
      mean: 65459.9 (0.998853)
      standard deviation: 2074.33 (0.0316522)
      kurtosis: 761.498
      skewness: -27.621
    Blue:
      min: 16962 (0.258824)
      max: 65535 (1)
      mean: 65471.6 (0.999033)
      standard deviation: 1750.22 (0.0267067)
      kurtosis: 761.498
      skewness: -27.621
    Alpha:
      min: 0 (0)
      max: 65535 (1)
      mean: 237.286 (0.00362075)
      standard deviation: 3936.27 (0.0600637)
      kurtosis: 271.189
      skewness: -16.5284


And both convert commands seem to produce 8-bit results:


Code: Select all
Image: pdf_with_transparency2.png
  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 3226x2592+0+0
  Resolution: 72x72
  Print size: 44.8056x36
  Units: Undefined
  Type: PaletteMatte
  Endianess: Undefined
  Colorspace: RGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
    alpha: 1-bit
  Channel statistics:
    Red:
      min: 211 (0.827451)
      max: 255 (1)
      mean: 254.943 (0.999775)
      standard deviation: 1.58536 (0.00621709)
      kurtosis: 760.555
      skewness: -27.6198
    Green:
      min: 31 (0.121569)
      max: 255 (1)
      mean: 254.708 (0.998853)
      standard deviation: 8.07131 (0.0316522)
      kurtosis: 761.498
      skewness: -27.621
    Blue:
      min: 66 (0.258824)
      max: 255 (1)
      mean: 254.753 (0.999033)
      standard deviation: 6.81021 (0.0267067)
      kurtosis: 761.498
      skewness: -27.621
    Alpha:
      min: 0 (0)
      max: 255 (1)
      mean: 0.923293 (0.00362075)
      standard deviation: 15.3162 (0.0600637)
      kurtosis: 271.189
      skewness: -16.5284


My delegates.xml file shows:

<delegate decode="ps:alpha" stealth="True" command="&quot;gsc&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pngalpha&quot; -dTextAlphaBits=%u -dGraphicsAlphaBits=%u &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;"/>

as I mentioned above.
User avatar
fmw42
 
Posts: 14888
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: PDF to PNG transparency conversion problem

Postby anthony » 2011-08-30T16:40:28+00:00

When you start getting this involved with PDF to image conversion you may be better off using ghostscript directly, and then feed the results to IM. That will avoid much of the 'security measures' that IM has to implement.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
http://www.imagemagick.org/Usage/
User avatar
anthony
 
Posts: 8731
Joined: 2004-05-31T19:27:03+00:00
Location: Brisbane, Australia

Re: PDF to PNG transparency conversion problem

Postby fmw42 » 2011-08-31T15:34:36+00:00

I am wondering if you might not have two or more versions of IM installed and they are getting confused with which delegates.xml file to use. You might search for all the delegates.xml files and be sure you don't have more than one that might be in your path.

find /usr | grep delegates.xml
User avatar
fmw42
 
Posts: 14888
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: PDF to PNG transparency conversion problem

Postby jfplante » 2012-04-27T19:50:35+00:00

Thanks Fred and Anthony, and sorry for the very late reply. I am just now starting back on this problem and hoping to solve that.

Fred, as you mentioned, I had 2 versions of IM installed and a plethora of delegates. However, i tracked down the one that was really used and made sure the driver was indeed pngalpha. And by using "-verbose", I can see the device used is, once again, pngalpha.

Code: Select all
"gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=2147483647 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72"  "-sOutputFile=/tmp/magick-OhZmAswB-%08d" "-f/tmp/magick-C7Aoc04D" "-f/tmp/magick-PgxJGEDG"
/tmp/magick-OhZmAswB-00000001 PNG 3226x2592 3226x2592+0+0 8-bit DirectClass 41.9KB 0.620u 0:00.629
pdf_with_transparency.pdf PDF 3226x2592 3226x2592+0+0 16-bit DirectClass 41.9KB 0.000u 0:00.000
pdf_with_transparency.pdf=>pdf_with_transparency.png PDF 3226x2592 3226x2592+0+0 8-bit PseudoClass 37c 8.19KB 1.090u 0:01.089


I tried using ghostscript directly, as per Anthony's suggestion, still no luck. I tried another setup (ubuntu), installing the latest ghostscript version so far (9.05), still this annoying small white box around those 2 red shapes. Also tried converting the original file (which was pdf 1.4) to further versions as I read pngalpha and pdf1.4 were not best friends: nothing.

What gives me hope though, is your setup Fred is working. Did you compile ghostscript with any specific settings? Does the conversion work for you when using ghostscript directly with that command?
Code: Select all
gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -sOutputFile=pdf_with_transparency.png pdf_with_transparency.pdf
jfplante
 
Posts: 10
Joined: 2011-08-15T13:26:13+00:00

Re: PDF to PNG transparency conversion problem

Postby fmw42 » 2012-04-27T19:56:37+00:00

I really don't know much about GS. But I ran your command


gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -sOutputFile=pdf_with_transparency.png pdf_with_transparency.pdf

GPL Ghostscript 9.05 (2012-02-08)
Copyright (C) 2010 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Processing pages 1 through 1.
Page 1

The image I got was a large transparent image with a small white square (in the center) with a red box and circle inside.

I expect you need to flatten this onto a white background to get it to look like the pdf.
User avatar
fmw42
 
Posts: 14888
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Re: PDF to PNG transparency conversion problem

Postby jfplante » 2012-04-28T13:41:10+00:00

Thanks Fred,

So we actually end up with the same result, I do end up with that annoying white rectangle. The pdf is supposed to be a red circle and rectangle superimposed over a transparent background. No white at all (you can see that when you set acrobat to show transparency, or open it in illustrator). I'll keep trying different things and post here if I find any solution.

Jf
jfplante
 
Posts: 10
Joined: 2011-08-15T13:26:13+00:00

Re: PDF to PNG transparency conversion problem

Postby jfplante » 2012-04-28T13:42:24+00:00

By the way, is this the same output you have when using the convert command from pdf to png?
jfplante
 
Posts: 10
Joined: 2011-08-15T13:26:13+00:00

Re: PDF to PNG transparency conversion problem

Postby fmw42 » 2012-04-28T14:58:16+00:00

jfplante wrote:By the way, is this the same output you have when using the convert command from pdf to png?


Yes. This is just a guess, but it might be like Adobe Photoshop background transparency vs alpha channel transparency. IM does not know how to deal with background transparency and can only deal with alpha transparency. You may have an image with white box around the red, but then it has background transparency everywhere but the red. Or there is both kinds of transparency with the alpha channel being everything but the red and white box and IM sees only this.

On the otherhand, Photoshop, displays it the same as Acrobat and I see no alpha transparency, just background transparency.


However, this will convert it correctly, though a fudge.

convert pdf_with_transparency.pdf -transparent white pdf_with_transparency.png
User avatar
fmw42
 
Posts: 14888
Joined: 2007-07-02T17:14:51+00:00
Location: Sunnyvale, California, USA

Next

Return to Users

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 16 guests