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!
How to get real-world output size from arbitrary input
-
- 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
convert in.png -resample 96 out.png
snibgo's IM pages: im.snibgo.com
Re: How to get real-world output size from arbitrary input
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
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
Re: How to get real-world output size from arbitrary input
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.)
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.)
Re: How to get real-world output size from arbitrary input
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
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
-
- 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
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?I'm expecting the input size of 1016x1306 (at 120 PixelsPerCentimeter) to become 255x328 (at 96 PixelsPerInch).
Your IM is rather old. An upgrade may help.
snibgo's IM pages: im.snibgo.com
Re: How to get real-world output size from arbitrary input
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.
)
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.

Re: How to get real-world output size from arbitrary input
An upgrade to 6.6.3 did solve the problem.
And, yes, my arithmetic was flawed. Sorry!
And thanks for the all the help.
And, yes, my arithmetic was flawed. Sorry!
And thanks for the all the help.