Reputation: 2674
I have hundreds of images which width and height may vary. I want to resize all of them with this requirements:
Make them smaller so the largest side should get 2560px or the shortest size should be 1600px. Right now I'm using this code:
for file in ls
; do convert $file -resize "2560>" new-$file;done
for file in ls
; do convert $file -resize "1600^>" new-$file;done
Never the largest side should be less than 2560 or the shortest size less than 1600px
It would be awesome to crop extra space so my final images should be 2560x1600 (landscape) or 1600x2560 (portrait).
For instance if my image is 4000x3000, I may get 2560x1920 or 2133x1600. I would like to keep 2560x1920 and crop 160 pixels from both top and bottom of it to get 2560x1600.
Code I use right now is this:
for i in `ls`; do convert $i -resize '2560x1600^' -gravity center -crop '2560x1600+0+0' new-$i; done
But if my image is 3000x4000 (portrait mode), I may get 2560x3413 and then it crops till I get 2560x1600 where I would want 1600x2560.
Upvotes: 0
Views: 1115
Reputation: 2674
It seems a script is needed. This is my solution based in previous comment:
#!/bin/bash
# Variables for resize process:
shortest=1600;
longest=2560;
# Ignore case, and suppress errors if no files
shopt -s nullglob
shopt -s nocaseglob
# Process all image files
for f in *.gif *.png *.jpg; do
# Get image's width and height, in one go
read w h < <(identify -format "%w %h" "$f")
if [ $w -eq $h ]; then
convert $f -resize "${shortest}x${shortest}^" -gravity center -crop "${shortest}x${shortest}+0+0" new-$f
elif [ $h -gt $w ]; then
convert $f -resize "${shortest}x${longest}^" -gravity center -crop "${shortest}x${longest}+0+0" new-$f
else
convert $f -resize "${longest}x${shortest}^" -gravity center -crop "${longest}x${shortest}+0+0" new-$f
fi
done
Upvotes: 0
Reputation: 207345
I would suggest you use a script like this to get each image's dimensions. Then you can implement any logic you wish according to the image size - and also avoid parsing the output of ls
which is generally considered a bad idea.
#!/bin/bash
# Ignore case, and suppress errors if no files
shopt -s nullglob
shopt -s nocaseglob
# Process all image files
for f in *.gif *.png *.jpg; do
# Get image's width and height, in one go
read w h < <(identify -format "%w %h" "$f")
if [ $w -eq $h ]; then
echo $f is square at ${w}x${h}
elif [ $h -gt $w ]; then
echo $f is taller than wide at ${w}x${h}
else
echo $f is wider than tall at ${w}x${h}
fi
done
Output:
lena.png is square at 128x128
lena_fft_0.png is square at 128x128
lena_fft_1.png is square at 128x128
m.png is wider than tall at 274x195
1.png is taller than wide at 256x276
2.png is taller than wide at 256x276
Upvotes: 3