PDF to PNG with 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?".
emptyofwhat

PDF to PNG with transparency

Post by emptyofwhat »

Hello all!

I have a PDF that I need to convert to a png with the transparency intact. I can convert the PDF to a png with transparency using PixelMater, but I can't seem to find what flags to set to get it to work with ImageMagick.

I have the latest version of both imagemagick and ghostscript for OSX.

Can anybody offer me any advice on how to construct the proper command?

Thank You!

eow
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG with transparency

Post by fmw42 »

try

convert readoptions image.pdf PNG32:image.png

or if that does not work

try adding -channel rgba -alpha on

convert readoptions image.pdf -channel rgba -alpha on PNG32:image.png


don't know if that will help.

Most PDF info is at the following but I don't see anything about transparency

http://www.imagemagick.org/Usage/formats/#pdf

and PNG info is at

http://www.imagemagick.org/Usage/formats/#png

readoptions at

http://www.imagemagick.org/Usage/basics/#cmdline
emptyofwhat

Re: PDF to PNG with transparency

Post by emptyofwhat »

Thanks for your reply, I tried following your suggestions, but I still can't get it to work.

I amy not be fully understanding what you meant by "readoptions" but I think you meant to try adding the commands "before" the file is input, I tried that, but no luck

Code: Select all

convert -channel rgba -alpha on another.pdf -channel rgba -alpha on PNG32:another.png
I also tried going directly to GhostScript, and it did read in the back ground as being transparent but it left a white box around the image which is exactly what I don't want.

makes me sad :(

eow
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG with transparency

Post by fmw42 »

readoptions are mostly like -colorspace or density for PDF
convert -channel rgba -alpha on another.pdf -channel rgba -alpha on PNG32:another.png
try either

convert another.pdf PNG32: another.png

or

convert another.pdf -channel rgba -alpha on PNG32: another.png


(you can use -matte in place of -alpha on)

can you post a link to one of your pdf files with transparency so we can try working with it?

otherwise wait for Anthony or Magick to reply
emptyofwhat

Re: PDF to PNG with transparency

Post by emptyofwhat »

Thanks again!


Here is the link to one of the PDFs
another.pdf
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG with transparency

Post by fmw42 »

According to the verbose info there is no alpha channel on this file.

identify -verbose another.pdf
Image: another.pdf
Format: PDF (Portable Document Format)
Class: DirectClass
Geometry: 1440x1152+0+0
Resolution: 72x72
Print size: 20x16
Units: Undefined
Type: TrueColor
Endianess: Undefined
Colorspace: RGB
Depth: 16/8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
red:
min: 0 (0)
max: 65535 (1)
mean: 49702.6 (0.758413)
standard deviation: 22176.2 (0.338388)
kurtosis: -0.833899
skewness: -0.964968
green:
min: 0 (0)
max: 65535 (1)
mean: 50283.5 (0.767278)
standard deviation: 19884.8 (0.303423)
kurtosis: -0.9811
skewness: -0.808098
blue:
min: 0 (0)
max: 65535 (1)
mean: 51690.6 (0.788748)
standard deviation: 18125.4 (0.276575)
kurtosis: -0.304608
skewness: -0.97397
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PDF to PNG with transparency

Post by magick »

Edit delegates.xml and change pnmraw to pngalpha. Now convert your PDF. It only works for the first page of PDF because PNG limits one page per image file. What we need is a Ghostscript driver that supports RGB, RGBA, CMYK, and CMYK with more than one page per image file.
emptyofwhat

Re: PDF to PNG with transparency

Post by emptyofwhat »

Thanks! This is closer, but still not what I need.

I change the delegates.xml file as instructed and tried to convert but There is still some white left around the images that should not be there.

Here is a link to the PNG that i can create by opening the PDF in Pixelmator and saving as a PNG

another.png ( Transparency shows up as black in this viewer. Download the file to see the transparency )

that is what I am going for

Thanks again for everyone's help :>)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG with transparency

Post by fmw42 »

please clarify how you expect to get transparency in your output png when your input pdf has no transparency (after downloading and reviewing with identify -verbose ...). Where is the transparency?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PDF to PNG with transparency

Post by magick »

Agreed. Your image is surrounded by white, it is not transparent. You can replace the white with the -floodfill option.
emptyofwhat

Re: PDF to PNG with transparency

Post by emptyofwhat »

Thanks again everyone.

I understand that verbose is saying that there is no transparency, but I am pretty certain there is. What I haven't told you is that the original images that went into that PDF were PNGs with transparencies, There was no white anywhere. The PDF was created by laying those images out in a grid, and rendering the result to PDF.

Also Check this Movie

Both of those were exactly the same PDF file. one was opened in Preview and the was opened in Pixelmator. Pixilemator correctly displayed the transparency.

Am I just nuts, or does this suggest that the full transparency is in there somewhere?

thanks

eow
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG with transparency

Post by fmw42 »

I understand that verbose is saying that there is no transparency, but I am pretty certain there is. What I haven't told you is that the original images that went into that PDF were PNGs with transparencies, There was no white anywhere. The PDF was created by laying those images out in a grid, and rendering the result to PDF.
So what tool did you use to lay the images in a grid and render to PDF. Somewhere along the line the transparency has been removed. Either it happened when you made the PDF or when you uploaded to your server for us to download. But the bottom line is that after downloading from your server, the file has NO transparency as indicated by identify. Unless identify cannot detect transparency in PDF files?

OK doing a test:

convert rose: -channel rgba -alpha on rose.pdf
identify -verbose rose.pdf
Image: rose.pdf
Format: PDF (Portable Document Format)
Class: DirectClass
Geometry: 70x46+0+0
Resolution: 72x72
Print size: 0.972222x0.638889
Units: Undefined
Type: TrueColor
Endianess: Undefined
Colorspace: RGB
Depth: 16/8-bit
Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
Channel statistics:
red:
min: 8995 (0.137255)
max: 65535 (1)
mean: 37448 (0.57142)
standard deviation: 17808.9 (0.271746)
kurtosis: -1.385
skewness: 0.14637
green:
min: 5654 (0.0862745)
max: 65535 (1)
mean: 22939.9 (0.35004)
standard deviation: 13484.7 (0.205764)
kurtosis: 2.63898
skewness: 1.81893
blue:
min: 6168 (0.0941176)
max: 65535 (1)
mean: 20680.4 (0.315562)
standard deviation: 14163.6 (0.216123)
kurtosis: 3.01818
skewness: 1.9795


This seems to indicate that either the rose image pdf did not get the alpha channel or identify cannot detect it if it is there.

However, taking Magick's suggestion, I edited delegates.xml and changed pnmraw to pngalpha and repeated

convert rose: -channel rgba -alpha on rose.pdf
identify -verbose rose.pdf
Image: rose.pdf
Format: PDF (Portable Document Format)
Class: DirectClass
Geometry: 70x46+0+0
Resolution: 72x72
Print size: 0.972222x0.638889
Units: Undefined
Type: TrueColorMatte
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: 8995 (0.137255)
max: 65535 (1)
mean: 37448 (0.57142)
standard deviation: 17808.9 (0.271746)
kurtosis: -1.385
skewness: 0.14637
green:
min: 5654 (0.0862745)
max: 65535 (1)
mean: 22939.9 (0.35004)
standard deviation: 13484.7 (0.205764)
kurtosis: 2.63898
skewness: 1.81893
blue:
min: 6168 (0.0941176)
max: 65535 (1)
mean: 20680.4 (0.315562)
standard deviation: 14163.6 (0.216123)
kurtosis: 3.01818
skewness: 1.9795
alpha:
min: 65535 (1)
max: 65535 (1)
mean: 65535 (1)
standard deviation: -0 (-0)
kurtosis: 0
skewness: 0

so the rose pdf now shows its alpha

so now going back to your image.

identify -verbose another.pdf
Image: another.pdf
Format: PDF (Portable Document Format)
Class: DirectClass
Geometry: 1440x1152+0+0
Resolution: 72x72
Print size: 20x16
Units: Undefined
Type: TrueColorMatte
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: 0 (0)
max: 65535 (1)
mean: 49630.9 (0.757318)
standard deviation: 22125.2 (0.33761)
kurtosis: -0.831107
skewness: -0.966827
green:
min: 0 (0)
max: 65535 (1)
mean: 50211.8 (0.766183)
standard deviation: 19830 (0.302587)
kurtosis: -0.976927
skewness: -0.810271
blue:
min: 0 (0)
max: 65535 (1)
mean: 51618.8 (0.787653)
standard deviation: 18070.8 (0.275743)
kurtosis: -0.2945
skewness: -0.977763
alpha:
min: 0 (0)
max: 65535 (1)
mean: 47237.6 (0.7208)
standard deviation: 29399.4 (0.448606)
kurtosis: -1.031
skewness: 0.98438


So now it shows an alpha.

so

convert another.pdf PNG32:another.png
or
convert another.pdf -channel rgba -alpha on PNG32:another.png

identify -verbose another.png
Image: another.png
Format: PNG (Portable Network Graphics)
Class: DirectClass
Geometry: 1440x1152+0+0
Resolution: 72x72
Print size: 20x16
Units: Undefined
Type: TrueColorMatte
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: 0 (0)
max: 255 (1)
mean: 193.116 (0.757318)
standard deviation: 86.0904 (0.33761)
kurtosis: -0.831107
skewness: -0.966827
green:
min: 0 (0)
max: 255 (1)
mean: 195.377 (0.766183)
standard deviation: 77.1597 (0.302587)
kurtosis: -0.976927
skewness: -0.810271
blue:
min: 0 (0)
max: 255 (1)
mean: 200.851 (0.787653)
standard deviation: 70.3146 (0.275743)
kurtosis: -0.2945
skewness: -0.977763
alpha:
min: 0 (0)
max: 255 (1)
mean: 183.804 (0.7208)
standard deviation: 114.395 (0.448606)
kurtosis: -1.031
skewness: 0.98438


So now the alpha is carried from the PDF to the PNG.


But displaying your PDF or PNG in Mac Preview shows no apparent alpha or totally opaque alpha.

So I am still confused about your alpha. The stats above say that the alpha is 1bit (on or off in value) and its channel mean=183 or 72% opaque, but I see no evidence of it in either the PDF or PNG. What has been masked?


If I extract your alpha channel

convert another.pdf -alpha extract another_alpha.png

It is just the rectangular outside of the white area of your image that is transparent. Not really much need for this type of alpha channel is there or am I missing something here? It is not like the people are masked out!
emptyofwhat

Re: PDF to PNG with transparency

Post by emptyofwhat »

Well in my tests some of the background is now transparent, but not the part immediately surrounding the photos.

it could very well be that there is some masking involved. If you open the PDFs in illustrator you can find all all the images there with there transparencies intact. And a bunch of clipping masks all grouped together.

I am really a newbie at this image stuff so I am kind of just groping about.
emptyofwhat

Re: PDF to PNG with transparency

Post by emptyofwhat »

It is just the rectangular outside of the white area of your image that is transparent. Not really much need for this type of alpha channel is there or am I missing something here? It is not like the people are masked out!
I think that is the point, the people are masked out! At least that is my assertion. My evidence is that if you open the PDF in Illustrator you can see the individual images, and you move them about! And if you open the pdf with Pixelmator, the people are masked out! See the movie link I added in the earlier post.

It is it possible that there are TWO levels of stuff in that image, and ImageMagick is only seeing ONE?

I just don't enough (so frustrating)

:<(
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PDF to PNG with transparency

Post by fmw42 »

It is also possible that your processing has changed something or your uploading? When I open it in Acrobat, I see only the one. Perhaps you have multiple layers of transparency and only one is getting saved. Did it come from Photoshop?

Send me a direct copy of your pdf to fmw at alink dot net, if you want and I will look at it.
Post Reply