ImageMagick Convert -resize not quite working right(one image is bigger than the rest)Mar 24th, 2012 | By admin | Category: Linux / Freebsd
So I do a LOT of resizing and image converting, especially from jpg’s to PDF’s. Easily the easiest way to convert a series of images into a pdf file is with imagemagick. A simple command like convert *.jpg yourFile.pdf is all you need to convert a ton of jpegs into one neatly packed PDF but today I ran into a little problem. I resized all of my images first to be 850 px wide and then went to convert all of the images into a pdf but when I opened the PDF one of the pages was way bigger than the rest. So I figured that I must have resized them incorrectly so I did it again, converted to a pdf again and same bloody thing, page 4 was way bigger than the rest of them pages. At this point I figured something was up so I opened all of the pages in GIMP and was surprised to see that in gimp they were all the same size. Ugh. For a moment I contemplated just using libreoffice to made a PDF quickly but that’s not my style, i’d rather slave over it for 30 minutes to figure it out and then blog about it for another 10 instead of taking the easy way out 🙂
So what was wrong?
Well little did I know that there are two sizes of an image.. There’s the image dimensions 850x in my case and then there’s the print size, which is measured in inches and is based on the dpi or the density of the image. It would appear that the PDF reader I was using pays more attention to the latter attribute than the first.
So in order to find what the print size of my trouble image was I used the identify command with a -verbose switch(ie: identify -verbose image.jpg) and it brought up all sorts of information, with a little | grep print magic I found the print size sitting at over twice the size of the other images. This had to do with the fact that the dpi for my trouble image was 72×72 and 300×300 for every other image in the series.
How to fix it?
The fix is uber easy, I suppose you could bundle all of this together into one command when you are making your pdf but what you are looking at is the -density switch. For instance if you wanted to adjust the dpi of the image in question from 72×72 to 300×300 it would be as simple as typing: mogrify -density 300×300 image.jpg
However if you wanted to streamline the entire process then you can bundle all of your switches together and go:
convert -resize 850x -density 300×300 *.jpg yourPDF.pdf
and it will ensure that all of the images going into this pdf are 850 wide and with a dpi of 300×300 ensuring their print sizes all line up.
Hopefully that helps you out, imagemagick is the bomb.com and if your unfortunate enough to have to do a lot of image converting and such in a windows environment it’s definitely cause to install cwgwin or get imagemagick for windows(I think that exists)