Komsur
Komsur

Reputation: 11

Imagemagick: How to get image width, then use that to batch resize all images in folder to width x width / 2?

I have over 400 images I need resizing. They need to be their individual original widths, but the heights need to be exactly half the width of each image.

I've been trying for 3 hours to get something work in Imagemagick, with no luck.

Any ideas? Thanks a lot!

magick 40inch_downbelow_001.jpg -ping -resize "x%w/2" 40inch_downbelow_0aaaaaaa01.jpg - This is my current command, which is just a test. This spits out an image of exactly the same size, despite telling it to use half the width.

Upvotes: 0

Views: 1504

Answers (2)

GeeMack
GeeMack

Reputation: 5395

Here are two ways to accomplish this in Windows, one using ImageMagick v6 and another using v7. These will run all the JPG images in a directory.

Using IMv6...

for %I in ( *.jpg ) do (
   convert "%I" -set filename:0 "%[t]XXX.%[e]" ^
   +distort SRT "0,0 1,%[fx:w/h/2] 0" -shave 1 "%[filename:0]"
)

Using IMv7...

for %I in ( *.jpg ) do (
   magick "%I" -set filename:0 "%[t]XXX.%[e]" -resize "x%[fx:w/2]!" "%[filename:0]"
)

Substitute your file name modifier for the "XXX" I used in the examples. To use these commands in a BAT script you'll need to double the percent signs "%%".

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 208043

I think you want this, for a single image:

magick input.png -resize "x%[fx:int(w/2)]\!" result.png

The fact there is nothing before the x means the width is left unchanged. The new height is calculated as half the width and the exclamation mark permits distortion of the original aspect ratio.

If you want to do all PNGs in the current directory, use:

#!/bin/bash

for f in *.png; do
   magick "$f" -resize "x${h}\!" "$f"
done

If you only have old, v6 ImageMagick, go with:

#!/bin/bash

for f in *.png; do
   # Get current width and halve it
   w=$(identify -format "%w" "$f")
   ((h=w/2))
   mogrify -resize x${h}\! "$f"
done

Upvotes: 1

Related Questions