Page 1 of 1

Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T11:09:35-07:00
by ECMGuru
I want to use ImageMagick through a command line for achieving the following requirements:
1. Convert .TIF to .JPG
2. Resize such that the dimensions (width & height) are within a threshold.
3. Preserve physical dimensions (width & height in inches).

For example:
If the input file is a TIFF image with 3000x2000 dimension and 300 DPI resolution (i.e. 10 inches x 6.67 inches) and the dimension threshold is 2000 (the longer side shouldn’t be more than this), the output should be a JPEG image with 2000x1333 dimension and 200 DPI resolution (i.e. 10 inches x 6.67 inches - preserved)

My problem:

Code: Select all

Convert input.tif –resize “2000x2000>” output.jpg
Gives me an image of 2000x1333, but of 300 dpi (i.e. 6.67 inches x 4.44 inches)

Code: Select all

Convert input.tif –resample 200x200 output.jpg
Gives me an image of 2000x1333, and 200 DPI resolution (i.e. 10 inches x 6.67 inches - preserved). So this is perfect! But how would I know the desired density (in this case, 200x200) at runtime? All I know is the desired threshold in pixel (in this case, 2000).

Will you please help me in finding an appropriate command / combination of commands for achieving this? You can also suggest a mechanism for calculating the desired density based on the image dimensions and set threshold and using it in resample in a single command line.

Thank you.

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T11:54:03-07:00
by snibgo
You probably need to do some scripting before the convert command. See a thread with a similar problem viewtopic.php?f=1&t=24436

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T15:51:20-07:00
by fmw42
for linux/mac platforms try

density=`convert input.tiff -format " %x,%y" info:`
units=`convert input.tiff -format " %U" info:`
convert input.tif –resize “2000x2000>” -density $density -units $units output.jpg

see string formats
http://www.imagemagick.org/script/escape.php

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T17:00:26-07:00
by ECMGuru
Thank you, snibgo. But I was looking for a DOS script (preferably a single-line command) to achieve my requirements.

Thank you, fmw42. But this didn't help me. This is the same as Convert input.tif –resize “2000x2000>” output.jpg for me as it is giving me an image of 2000x1333, but of 300 dpi (i.e. 6.67 inches x 4.44 inches - changed the physical size - I want to retain it). So in this example, I want an image of 2000x1333, and 200 DPI resolution. But this 200 DPI has to be calculated at runtime with something like:

(Desired DPI) = (Source DPI) * (Threshold) / (Longer Length of Source)

Thus, in this example

(Desired DPI) = 300 * 2000 / 3000 = 200

Any help is appreciated.

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T17:24:57-07:00
by fmw42
Windows syntax is different from IM for getting the variables and using them. You need help from one of the Windows developers to convert my unix syntax to do the same as I did. IM 6 will not do such calculations in-line, but it should be possible in IM 7 (under alpha development right now).

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T18:17:01-07:00
by snibgo
fmw's script doesn't seem to change the image resolution.

I don't think this can be done in a single DOS command or a single convert.

I don't think there is an fx: symbol for the original image size, so I get it in a separate identify.

"%U" is a fairly recent addition to IM. x.tiff is just a sample file.

Code: Select all

%IM%convert -size 3000x2000 -density 300 xc: x.tiff

set RES=-resize "2000x2000"

FOR /F "usebackq" %%L ^
IN (`%IMG6875%identify -ping -format "WW=%%w\nHH=%%h" x.tiff`) ^
DO set %%L

FOR /F "usebackq" %%L ^
IN (`%IMG6875%convert x.tiff %RES% -format "RX=%%[fx:int(resolution.x*w/%WW%+0.5)]\nRY=%%[fx:int(resolution.y*h/%HH%+0.5)]\nUNITS=%%U" info:`) ^
DO set %%L


%IMG6875%convert x.tiff %RES% -density %RX%x%RY% -units %UNITS% y.tiff

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T18:47:23-07:00
by fmw42
I don't think there is an fx: symbol for the original image size, so I get it in a separate identify.
Perhaps I misunderstand your statement.

String formats %w and %h are the image size in pixels. %x and %y are the densities (sometimes called resolution)

In fx, you can get the image dimensions from

convert image -format "%[fx:w]" info:
convert image -format "%[fx:h]" info:

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T19:28:04-07:00
by snibgo
Yes. I used w and h inside %[fx:] in my script above.

I don't think the original width and height (before the resize) is available within a %[fx:]. It is available as two components of %G:

Code: Select all

convert -size 100x100 xc: -resize 10% -format "%G %w %h %W %H" info:
100x100 10 10 10 10

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T20:03:19-07:00
by fmw42
Sorry, I missed the issue about finding the size after resizing.

But my understanding was that he wanted to keep the original density. That was why I was using %x and %y and %U, so he could transfer it to his resulting image.

Perhaps I misunderstand his problem.

I do not see why

Convert input.tif –resample “2000x2000>” output.jpg

is not doing what he wants -- it should produce a smaller image with a corresponding smaller density, so that the resulting image prints to the same physical dimensions as before (pixels/density=inches).

3000pixels / 300 pixelsperinch = 10 inches

2000pixel / 200 pixelsperinch = 10 inches



Am I misunderstanding what he wants?


The issue I am getting is that doing the resample never finishes.


convert -size 3000x3000 xc:white -density 300 -units pixelsperinch 1tmp1.jpg

identify 1tmp1.jpg

Image: 1tmp1.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: PseudoClass
Geometry: 3000x3000+0+0
Resolution: 300x300
Print size: 10x10
Units: PixelsPerInch

Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 1-bit


convert 1tmp1.jpg -resample 2000x2000 1tmp2.jpg

has to be aborted since it does not finish for me with IM 6.8.7.9 Q16. So I cannot check the density.

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T20:21:22-07:00
by snibgo
The parameters to "-resample" are the desired resolution (always in DPI, I suppose). I don't think the OP knows the desired resolution, but does know the desired number of pixels. But I could be wrong about what is wanted.

Re: Resample to New Density Preserving PHYSICAL dimensions

Posted: 2013-12-11T20:33:45-07:00
by fmw42
snibgo wrote:The parameters to "-resample" are the desired resolution (always in DPI, I suppose). I don't think the OP knows the desired resolution, but does know the desired number of pixels. But I could be wrong about what is wanted.
You are right. I always thought it was pixels. Learned something new today. I never use -resample


Testing:


convert -size 600x600 xc:white -density 300 -units pixelsperinch 1tmp1.jpg

Image: 1tmp1.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: PseudoClass
Geometry: 600x600+0+0
Resolution: 300x300
Print size: 2x2
Units: PixelsPerInch

Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 1-bit



convert 1tmp1.jpg -resample 400x400 1tmp2.jpg

Image: 1tmp2.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: PseudoClass
Geometry: 800x800+0+0
Resolution: 400x400
Print size: 2x2
Units: PixelsPerInch

Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 1-bit



convert 1tmp1.jpg -resample 200x200 1tmp3.jpg

Image: 1tmp3.jpg
Format: JPEG (Joint Photographic Experts Group JFIF format)
Mime type: image/jpeg
Class: PseudoClass
Geometry: 400x400+0+0
Resolution: 200x200
Print size: 2x2
Units: PixelsPerInch

Type: Bilevel
Base type: Bilevel
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit
Channel depth:
gray: 1-bit

Resample will preserve the print size by adjusting the pixel dimensions to conform to the density. This is what he wants. But it works by specifying the density and letting the pixel size adjust accordingly. Seems he wants to go the other way, adjust the pixel size and have the density adjust accordingly. I don't think IM has such a function. But as you mention, you can compute the desired density from the original density and the input and output pixels dimensions. Then feed that new density to -resample. Or do what I suggested and resize the image to the pixels dimensions and just set the density to what is needed. Both ways should work. But you have to do the calculations ahead of time.