Conversion of PNG with/without physical info to A4 PDF

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
quanza
Posts: 1
Joined: 2013-06-10T13:51:50-07:00
Authentication code: 6789

Conversion of PNG with/without physical info to A4 PDF

Post by quanza »

I have some scans (300 DPI) which I'd like to convert to A4 PDFs, however imagemagick seems to behave in an unexpected way if the PNG metadata contains physical info (pHYs chunk).

To reproduce, I created a 2479x3508 image at 300 DPI using Gimp. Next, I exported it twice, with and without Save resolution checked in the PNG export dialog.

Code: Select all

$ identify -verbose black_* | grep -A6 Image:
Image: black_nophys.png
  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 2479x3508+0+0
  Units: Undefined
  Type: Bilevel
  Endianess: Undefined
--
Image: black_phys.png
  Format: PNG (Portable Network Graphics)
  Class: DirectClass
  Geometry: 2479x3508+0+0
  Resolution: 118.11x118.11
  Print size: 20.9889x29.7011
  Units: PixelsPerCentimeter
PNG can only store units in pixels per meter - displayed by imagemagick as PixelsPerCentimeter - but it's approximately 300 DPI.

Now, converting both files to PDF using different imagemagick params:

With -page:

Code: Select all

$ convert black_nophys.png -page A4 test.pdf && pdfinfo test.pdf | grep 'Page size'
Page size:      595 x 842 pts (A4)
$ convert black_phys.png -page A4 test.pdf && pdfinfo test.pdf | grep 'Page size'
Page size:      142.8 x 202.08 pts
First image: converts to a page size of 595 x 842 (exactly as specified by imagemagick for -page A4).
Second image: results in an unexpected page size (even if PNG PixelsPerCentimeter -> DPI conversion causes some loss, I'd expect it to be at least an A4 approximate)

Without -page:

Code: Select all

$ convert black_nophys.png test.pdf && pdfinfo test.pdf | grep 'Page size'
Page size:      2479 x 3508 pts
$ convert black_phys.png test.pdf && pdfinfo test.pdf | grep 'Page size'
Page size:      594.96 x 841.92 pts (A4)
First image: as expected (no physical metadata present)
Second image: approximate A4 size, though slightly off (conversion?)

Am I incorrectly using Imagemagick? I would expect -page A4 to produce a 595x842 PDF regardless of pHYs presence. Is this a bug?

Some additional (failed) attempts at explicitly specifying source image physical dimensions:

Code: Select all

$ convert -units PixelsPerInch -density 300 black_phys.png -page A4 test.pdf && pdfinfo test.pdf | grep 'Page size'
Page size:      142.8 x 202.08 pts
$ convert -units PixelsPerInch -density 300 black_nophys.png -page A4 test.pdf && pdfinfo test.pdf | grep 'Page size'
Page size:      142.8 x 202.08 pts
$ convert black_nophys.png -set units PixelsPerInch -density 300 -page A4 test.pdf && pdfinfo test.pdf | grep 'Page size'
Page size:      142.8 x 202.08 pts
$ convert black_phys.png -set units PixelsPerInch -density 300 -page A4 test.pdf && pdfinfo test.pdf | grep 'Page size'
Page size:      142.8 x 202.08 pts
Version info:

Code: Select all

Version: ImageMagick 6.8.5-10 2013-06-07 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2013 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib fontconfig freetype gslib jng jp2 jpeg lcms lqr lzma openexr pango png ps rsvg tiff wmf x xml zlib
Post Reply