How to get real-world output size from arbitrary input

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
vepxistqaosani

How to get real-world output size from arbitrary input

Post by vepxistqaosani »

I need to provide output files at 'real world' size, but the input files vary in resolution, geometry, and units.

That is, I could have 1700x1000 @ 120 pixels per centimeter and 1700x1000 @ 600 pixels per inch. I want output at 96 dpi, so the former should be 535x314 and the latter should be 272x160.

Is there anyway to accomplish this with a single ImageMagick command? Given the limits of my application, I cannot introspect the files before running convert.

Thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to get real-world output size from arbitrary input

Post by snibgo »

convert in.png -resample 96 out.png
snibgo's IM pages: im.snibgo.com
vepxistqaosani

Re: How to get real-world output size from arbitrary input

Post by vepxistqaosani »

Thanks, but that only works for files with Units of PixelsPerInch.

Could it be that ImageMagick 6.5.9-2 2010-02-03 Q16 on Windows XP doesn't handle PixelsPerCentimeter correctly?

E.g.,

identify -verbose "Tall Thin.tif" | grep "Geometry\|Resolution\|Print\|Units"
Geometry: 1016x1306+0+0
Resolution: 120x120
Print size: 8.46667x10.8833
Units: PixelsPerCentimeter

convert "Tall Thin.tif" -resample 96 TallThin_096.jpg

identify -verbose "TallThin_096.jpg" | grep "Geometry\|Resolution\|Print\|Units"
Geometry: 813x1045+0+0
Resolution: 120x120
Print size: 6.775x8.70833
Units: PixelsPerCentimeter

Expected :: 255x328 @ 96
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: How to get real-world output size from arbitrary input

Post by Drarakel »

Use a "-units PixelsPerInch" option after reading the image, so that the former density value gets converted to inches first:
convert input -units PixelsPerInch -resample 96 output

(Note that in JPG files, you might need to strip some profiles afterwards if you want that the density info is displayed everywhere in the same way.)
vepxistqaosani

Re: How to get real-world output size from arbitrary input

Post by vepxistqaosani »

That sounds logical, but still doesn't work. I'm expecting the input size of 1016x1306 (at 120 PixelsPerCentimeter) to become 255x328 (at 96 PixelsPerInch).

An additional feature of the problem is that the same ImageMagick command has to handle PixelsPerInch inputs. Currently, I can handle inches but not centimeters.

So, the image is being scaled by 96/120, or 80%, even with the unit specifications and strip. Of course, I don't really care about the resolution, only the final size.

convert "Tall Thin.tif" -units PixelsPerInch -resample 96 -strip TallThin_096.jpg

identify -verbose "TallThin_096.jpg" | grep "Geometry\|Resolution\|Print\|Units"
Geometry: 813x1045+0+0
Resolution: 96x96
Print size: 8.46875x10.8854
Units: PixelsPerInch

convert "Tall Thin.tif" -units PixelsPerInch -resample 96 TallThin_096.jpg

identify -verbose "TallThin_096.jpg" | grep "Geometry\|Resolution\|Print\|Units"
Geometry: 813x1045+0+0
Resolution: 120x120
Print size: 6.775x8.70833
Units: PixelsPerCentimeter
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to get real-world output size from arbitrary input

Post by snibgo »

I'm expecting the input size of 1016x1306 (at 120 PixelsPerCentimeter) to become 255x328 (at 96 PixelsPerInch).
If my maths is correct: 1016 pixels at 120 PixelsPerCentimeter is 8.47cm or 3.33 inches. At 96 dpi, this is 320 pixels, not 255. Why do you expect 255?

Your IM is rather old. An upgrade may help.
snibgo's IM pages: im.snibgo.com
Drarakel
Posts: 547
Joined: 2010-04-07T12:36:59-07:00
Authentication code: 8675308

Re: How to get real-world output size from arbitrary input

Post by Drarakel »

I just saw the ImageMagick version. Yeah, updating is required.
In your older IM version, "-units PixelsPerInch" really only sets the units value. You have to use version 6.6.1-5 or newer - then that option converts the density before resampling starts.
(Of course you will still have problems if a false value is stored in the input file. :wink:)
vepxistqaosani

Re: How to get real-world output size from arbitrary input

Post by vepxistqaosani »

An upgrade to 6.6.3 did solve the problem.

And, yes, my arithmetic was flawed. Sorry!

And thanks for the all the help.
Post Reply