Reduce dpi but don't enlarge it in IM

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
jwilheim

Reduce dpi but don't enlarge it in IM

Post by jwilheim »

I am trying to figure out how to configure IM to change the dpi of an image only if it is larger than a desired value.
For example, if given any file with a dpi larger than 200, I want to change it to 200 dpi.
But if given a file of less than 200 dpi, I want to keep it at the original dpi.
Any help would be greatly appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Reduce dpi but don't enlarge it in IM

Post by fmw42 »

I believe that you will likely need to write a script to loop over each image and then test the resolution and then decide if you want to change it. See %[xresolution], %[yresolution] and %x and %y for x and y resolutions in http://www.imagemagick.org/script/escape.php You can also get density and units from the -verbose info:

xres=`convert image -format "%[xresolution]" info:`
if [ $xres > 200 ]; then
convert image -density 200 resultimage
fi

Note this is unix. For windows see http://www.imagemagick.org/Usage/windows/

You can also use "%x" in place of "%x" "%[xresolution]". This will give you both the resolution (density) and the units, but you will have to separate the two first. Units may be important as png units are pixelspercentimeter. So you may need both to convert to one common units such as pixelsperinch.
Post Reply