Converting from PNG to PDF and changing DPI

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
Dutchman
Posts: 4
Joined: 2012-05-22T07:14:21-07:00
Authentication code: 13

Converting from PNG to PDF and changing DPI

Post by Dutchman »

I'm trying to get the correct commands to get my converting process running. For this test I have a:

1200x1200px PNG, which I want to convert to PDF (1.3 or up) and have it print out at 600dpi. So without resampling that would mean I would have a pixel worth of data for each dot on the paper output. So it would be printed as a 2x2 inch image with the 1.44M pixels.

As this will be run on a webserver I need to minimize the CPU time and would like to prevent any resampling whenever possible. Which brings me to the first question:

I have several options, from which I don't know what is best to choose?
- change the dpi setting before converting to pdf
- change the dpi setting after conversion to pdf
- change the dpi setting during conversion to pdf


Then the actual commands which I've tried so far but not always giving the expected output

Setting the density before conversion I think should be done this way:

Code: Select all

$ convert -units PixelsPerInch 1200x1200px.png -density 600 1200x1200px-changedensity.png
$ identify -verbose 1200x1200px-changedensity.png |head -7
Image: 1200x1200px-changedensity.png
  Format: PNG (Portable Network Graphics)
  Class: PseudoClass
  Geometry: 1200x1200+0+0
  Resolution: 236.22x236.22
  Print size: 5.08001x5.08001
  Units: PixelsPerCentimeter
...
  Number pixels: 1.44M
  Pixels per second: 72MB
$ convert 1200x1200px-changedensity.png -format pdf 1200x1200px-changedensity.pdf
$ identify -verbose 1200x1200px-changedensity.pdf |head -7
Image: 1200x1200px-changedensity.pdf
  Format: PDF (Portable Document Format)
  Class: DirectClass
  Geometry: 144x144+0+0
  Resolution: 72x72
  Print size: 2x2
  Units: Undefined
...
  Filesize: 2.89KB
  Number pixels: 20.7K
  Pixels per second: 207.36TB
However the geometry has changed to lower values, the amount of pixel, resolution and units don't have the expected values, and there is a huge value at Pixels per second. On the other side, the printsize is as expected 2x2 inch. Did I loose data in this process?


Setting the density after the PDF conversion seems not only to change dpi setting, but also resample the image I think.

Code: Select all

$ convert 1200x1200px.png -format pdf 1200x1200px.pdf
$ identify -verbose 1200x1200px.pdf |head -7
Image: 1200x1200px.pdf
  Format: PDF (Portable Document Format)
  Class: DirectClass
  Geometry: 1200x1200+0+0
  Resolution: 72x72
  Print size: 16.6667x16.6667
  Units: Undefined
...
  Filesize: 11KB
  Number pixels: 1.44M
  Pixels per second: 36MB

$ convert -units PixelsPerInch 1200x1200px.pdf -density 600 1200x1200px-changedensity2.pdf
$ identify -verbose 1200x1200px-changedensity2.pdf |head -7
Image: 1200x1200px-changedensity.pdf
  Format: PDF (Portable Document Format)
  Class: DirectClass
  Geometry: 144x144+0+0
  Resolution: 72x72
  Print size: 2x2
  Units: Undefined
....
  Filesize: 2.89KB
  Number pixels: 20.7K
  Pixels per second: 207.36TB
Doing the whole thing in 1 go again seems to generate the correct output. Also the Pixels per second is much lower now:

Code: Select all

$ convert -set units PixelsPerInch 1200x1200px.png -density 600 -format pdf 1200x1200px-changedensity3.pdf
$ identify -verbose 1200x1200px-changedensity3.pdf |head -7
Image: 1200x1200px-changedensity3.pdf
  Format: PDF (Portable Document Format)
  Class: DirectClass
  Geometry: 144x144+0+0
  Resolution: 72x72
  Print size: 2x2
  Units: Undefined
...
  Filesize: 2.89KB
  Number pixels: 20.7K
  Pixels per second: 2.074MB

Ok, so far identify seems to show the correct output, but when looking at the filelevel I see this:

Code: Select all

$ ls -lah 1200x1200px-c*.pdf
-rw-r--r--  1 user  staff   5.3K May 22 16:55 1200x1200px-changedensity.pdf
-rw-r--r--  1 user  staff   4.2M May 22 17:14 1200x1200px-changedensity2.pdf
-rw-r--r--  1 user  staff   8.2K May 22 17:20 1200x1200px-changedensity3.pdf
Which show a huge 4 Megabyte file in the second try.

I also tried several other command, but it is still unclear which command give me the end result which I need without changing the data and loading my server.

P.S. this will be run/executed through PHP eventually, so any advice is also welcome. However I would like to get the needed commands correct at first.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting from PNG to PDF and changing DPI

Post by fmw42 »

PNG does not support pixels per inch. So I think you need to set that after reading the png. Also I am not sure what -format pdf is doing. You do need to set the density before writing the pdf and after reading the png. So I would try

convert 1200x1200px.png -density 600 -set units PixelsPerInch 1200x1200px-changedensity3.pdf

see
http://www.imagemagick.org/Usage/formats/#ps
Dutchman
Posts: 4
Joined: 2012-05-22T07:14:21-07:00
Authentication code: 13

Re: Converting from PNG to PDF and changing DPI

Post by Dutchman »

I used the -format pdf to define the output format, but I guess that function is depricated or something. And I noticed that PNG can hold the dpi data, but will not use it. That's why the first option works (change density before converting to pdf)

Looking at the identify output I'm still not sure if the outputfile is 600dpi. The amount of pixels has decreased from 1.44M to 20.7K. Is there any other way I can check if the output is 600dpi?

Code: Select all

$ identify -verbose 1200x1200px-changedensity4.pdf 
Image: 1200x1200px-changedensity4.pdf
  Format: PDF (Portable Document Format)
  Class: DirectClass
  Geometry: 144x144+0+0
  Resolution: 72x72
  Print size: 2x2
  Units: Undefined
...
  Artifacts:
    filename: 1200x1200px-changedensity4.pdf
    verbose: true
  Tainted: False
  Filesize: 2.89KB
  Number pixels: 20.7K
  Pixels per second: 207.36TB
  User time: 0.010u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.7.6-0 2012-04-16 Q16 http://www.imagemagick.org
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting from PNG to PDF and changing DPI

Post by fmw42 »

used the -format pdf to define the output format, but I guess that function is depricated or something.
That is only for mogrify and not convert
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting from PNG to PDF and changing DPI

Post by fmw42 »

I am not sure things are working correctly. IM 6.7.7.0 Q16 (hdri).

convert logo: -units pixelspercentimeter logo.png
identify -verbose logo.png
Image: logo.png
Format: PNG (Portable Network Graphics)
Class: PseudoClass
Geometry: 640x480+0+0
Resolution: 72x72
Print size: 8.88889x6.66667
Units: PixelsPerCentimeter
Type: Palette
Endianess: Undefined
Colorspace: sRGB
Depth: 8-bit


convert logo.png -density 600 -units pixelsperinch logo.pdf
identify -verbose logo.pdf
Image: logo.pdf
Format: PDF (Portable Document Format)
Class: DirectClass
Geometry: 77x58+0+0
Resolution: 72x72
Print size: 1.06944x0.805556
Units: Undefined
Type: PaletteAlpha
Endianess: Undefined
Colorspace: sRGB
Depth: 16/8-bit

PDF files are vector even if they have an imbedded image. So they do not carry any density or units. You have to set those when you read in the PDF to be whatever you want later.


But what puzzles me is why the PDF has been promoted from pseudoclass/palette to directclass/paletteAlpha. Why the alpha channel? The other thing is the print size change. If that took into account either density of 600 or the changes from pixelspercentimeter to pixelsperinch, the result would not be 1.06944x0.805556. Also this does not account for the pixel dimensions change from 640x480 to 77x58.

As I am not an expert on PNG or PDF, I think we need clarification from the IM developers or the PNG developer.
Dutchman
Posts: 4
Joined: 2012-05-22T07:14:21-07:00
Authentication code: 13

Re: Converting from PNG to PDF and changing DPI

Post by Dutchman »

Ok, I didn't see the difference in Pallet. But that maybe the root of my other problem I haven't talked about yet. I'll try to rung the commands and remove the Alpha channel, hopefully that will produce the expected Pallet setting.

All I know that PDF works with points and that my output geometry changes when I convert to PDF. But when doing a preflight in Acrobat, the PDF does contain the image in the expected dpi setting, and it does fill the whole PDF page.

At least.... with version 6.7.6. Running the command on my webserver gave me a huge Mediabox output. After some checks, the version on my server was a combination of 6.4.4 and 6.2.8. So I now upgraded the whole thing on my GoDaddy server and now the mediabox size is also correct.
Dutchman
Posts: 4
Joined: 2012-05-22T07:14:21-07:00
Authentication code: 13

Re: Converting from PNG to PDF and changing DPI

Post by Dutchman »

I did some tests to see what happens with the Type, but it produces different output at my end:

Original PNG (simple 1200x1200 pixel image created with Illustrator CS5 save for web):

Code: Select all

d$ identify -verbose 1200x1200px.png 
Image: 1200x1200px.png
  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 1200x1200+0+0
  Resolution: 72x72
  Print size: 16.6667x16.6667
  Units: Undefined
  Type: Palette
PNG density adjusted and converted to PDF:

Code: Select all

$ identify -verbose 1200x1200px.pdf
Image: 1200x1200px.pdf
  Format: PDF (Portable Document Format)
  Class: DirectClass
  Geometry: 144x144+0+0
  Resolution: 72x72
  Print size: 2x2
  Units: Undefined
  Type: PaletteMatte
PNG density adjusted, converted to PDF and alpha turned off:

Code: Select all

$ convert 1200x1200px.png -alpha off -density 600 -set units PixelsPerInch 1200x1200px-noalpha.pdf

$ identify -verbose 1200x1200px-noalpha.pdf 
Image: 1200x1200px-noalpha.pdf
  Format: PDF (Portable Document Format)
  Class: DirectClass
  Geometry: 144x144+0+0
  Resolution: 72x72
  Print size: 2x2
  Units: Undefined
  Type: PaletteMatte
The last version does produce the correct output from a functionality point of view.
Post Reply