Sergazon
Sergazon

Reputation: 33

How to resize images using ImageMagick? (mac os)

There is a pack of images and I want to reduce the height and width on 10 px of each image. The problem is that every image has a different size (i mean height and width). I found out how to resize images in terminal using ImageMagick, but it could resize images only to a fixed size (for example: convert example.png -resize 200x100 example.png). I need resizing to ((current width)-10px)x((current height)-10px) for every image. I am new to programming, be patient, please :)

Upvotes: 3

Views: 1923

Answers (1)

fmw42
fmw42

Reputation: 53202

If using Imagemagick 7, you can write a loop over all your images. In Unix syntax (assuming your images names have no spaces in them):

cd to your current directory
list=$(ls)
for img in $list; do
magick $img -resize "%[fx:w-10]x%[fx:h-10]" $img
done

If you do not want to overwrite your images, then create a new directory and put the path to that directory before the output.

If you want to do more than one image at a time, you can do:

cd to your current directory
magick * -resize "%[fx:s.w-10]x%[fx:s.h-10]" result.suffix

This will make the resulting images all with the same name, but with numbers appended, such as result-0.suffix, result-1.suffix, etc.

If you are using Imagemagick 6, then you will have to precompute the sizes in a separate command.

cd to your current directory
list=$(ls)
for img in $list; do
dimensions=$(convert image -format "%[fx:w-10]x%[fx:h-10]" info:)
convert $img -resize $dimensions $img
done

Note that resizing will not necessarily give you the result you want, since Imagemagick will try to keep the aspect ratio. So if you want to force the exact sizes, then you need to add ! to your resize argument. However, that will cause some distortion, though probably not too much for only resizing by 10 pixels.

An easier way would be to just shave off 5 pixels all around. Then you could do a whole folder using mogrify:

cd to current directory after creating a new directory for the output if desired:
mogrify -path path/to/new_directory -shave 5x6 *


In Imagemagick 7, that would be magick mogrify ...

Upvotes: 3

Related Questions