ImageMagick change image width and height

2019-04-14 03:44发布

问题:

I am using ImageMagick to resize image resolution by using below command-line option

convert abc.png -set units PixelsPerInch -density 75 abc_a.png

I am in need of this: if any images has more than 300 width OR more than 100 height, I want to convert it to width 300 width and 100 height, with changing above dpi (i.e. 75dpi).

Can any one help me on this?

回答1:

If you are on Linux/OSX, you can get the image dimensions like this:

identify -format "%w %h" input.jpg

So, if you want the width and height in variables w and h, do this:

read w h < <(identify -format "%w %h" input.jpg)

Now you can test the width and height and do further processing if necessary:

[ $w -gt 300 -o $h -gt 100 ] && convert input.jpg -set units ...

Or, if you want to be more verbose:

if [ $w -gt 300 -o $h -gt 100 ]; then
    convert ...
fi

So, the total solution I am proposing looks like this:

#!/usr/bin/bash
read w h < <(identify -format "%w %h" input.jpg)
[ $w -gt 300 -o $h -gt 100 ] && convert input.jpg -set units ...

JPEG or PNG makes no difference, so just replace my JPG with PNG if that is the format of your choice.

Updated for Windows

Ok, no-one else is helping so I will get out my (very) rusty Windows skills. Get the image width something like this under Windows:

identify -format "%w" input.png > w.txt
set /p w=<w.txt

Now get the height:

identify -format "%h" input.png > h.txt
set /p h=<h.txt

You should now have the width and height of image input.png in 2 variables, w and h, check by typing

echo %w%
echo %h%

Now you need to do some IF statements:

if %w% LEQ 300 GOTO SKIP
if %h% LEQ 100 GOTO SKIP
convert ....
:SKIP

Note:: You may need ^ in front of the percent sign in Windows.

Note: You may need double @ signs in scripts because Windows is illogical.



回答2:

You cannot control the -density and the width plus height at the same time!

Density (or resolution), when creating an image, will automatically resize the image to a certain number of pixels in width and height (or it will have been ignored).

Density (or resolution), when displaying an image (like in a browser window, within a HTML page, or on a PDF page), will not change the original images dimensions: instead it will zoom in or zoom out the respective view on the image.

Density (or resolution), when used in the metadata of an image (which is not supported by every file format), does not change the image dimensions -- it just gives a hint to the displaying software, at what zoom level the image wants to be displayed (which is not supported by every image viewer).


Now to your question...

Try this command:

 convert abc.png -scale 300x100\> abc_a.png

This will scale the image only if

  • either the original image's width is larger than 300 pixels,
  • or the original image's height is larger than 100 pixels.

The scaling will preserve the aspect ratio of the original image. -- Is this what you are looking for?

If the image is smaller, then no scaling will happen and abc_a.png will have the original dimensions.

If you want to *emphatically scale the image to 300x100, no matter what, and loose the aspect ratio, bearing with some distortion of the original image, the use:

convert abc.png -scale 300x100\! abc_b.png

(However, this will also scale smaller images...)