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?
If you are on Linux/OSX, you can get the image dimensions like this:
So, if you want the width and height in variables
w
andh
, do this:Now you can test the width and height and do further processing if necessary:
Or, if you want to be more verbose:
So, the total solution I am proposing looks like this:
JPEG
orPNG
makes no difference, so just replace myJPG
withPNG
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:
Now get the height:
You should now have the width and height of image
input.png
in 2 variables,w
andh
, check by typingNow you need to do some
IF
statements:Note:: You may need
^
in front of the percent sign in Windows.Note: You may need double
@
signs in scripts because Windows is illogical.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:
This will scale the image only if
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:
(However, this will also scale smaller images...)