PNG density with mogrify

ImageMagick wizardYou use DocBook so you can generate multiple output formats. Two of those formats are PDF and HTML. You take a few screenshots now and then. They look fine either in your PDF or your HTML, but not both. You RTFM, Bob Stayton’s online book covering the XSL stylesheets, but you do not want conditional markup and stylesheet customizations to deal with it all. You wake up at 5 a.m. wondering what to do.

And then you find this note towards the bottom of http://stackoverflow.com/questions/4261838/xsl-fo-image-size-issue-with-pdfs:

“@Ace, PNG files have DPI information embedded within them. You can manipulate that information for example with programs from the ImageMagick suite. (So, for example, if you have a 800 px wide PNG that you want to present as a 5 inch wide image, you could just change the DPI information accordingly by mogrify -density 160 -units PixelsPerInch image.png.) – Jukka Matilainen Nov 24 ’10 at 20:55″

Thanks so much, Jukka. Yours is some of the best doc I’ve read this week. Even the 160 density is right on for me. Plus, I learned something that I should have known but did not know about PDF.

I made a copy of my images directory, moved the original to images.orig, and then ran a one-liner.

$ for file in *.png; do mogrify -density 160 -units PixelsPerInch $file; done

The PDF now looks fine, no more overflowing screenshots. The HTML remains unaffected.

3 thoughts on “PNG density with mogrify

    1. Lana Frost

      Preview on Mac OS X 10.6.7. Tools > Adjust Size works well for me. Remember to deselect “Resample Image” otherwise it looks great in pdf but not so good in html. A width of 10cm is a good fit for a standard size pdf. A width of 10cm with a dpi of 140 worked for my particular image.

  1. Mike Jang

    While it may be implicit, the image adjustment is a multi-step process

    – If possible, create a screenshot that is bigger than needed
    – Open the image from a tool like Finder; this opens the image in Preview.
    – Click Tools -> Adjust Size
    – Deselect Resample Image
    – Adjust the size. If the suggested 10 cm is smaller than the original screenshot, that should automatically increase the Resolution (pixels / inch).

    (I should test if this works for sceenshots that have to be magnified)

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.