I'm trying to batch crop images in a folder. Whenever I try doing this loop
for image in '/home/donald/Desktop/Im'*.jpg; do
convert "$image" -gravity center -crop 95X95% "${image%.jpg}"-modified.jpg
done
I end up with the error:
convert.im6: unable to open image `/home/donald/Desktop/Im*.jpg': No such file or directory @ error/blob.c/OpenBlob/2638.
convert.im6: no images defined `/home/donald/Desktop/Im*-modified.jpg' @ error/convert.c/ConvertImageCommand/3044.
Which is bizarre because there are definitely images in the folder and according the meta-data they are jpg
When I run shopt | grep glob
I get this output:
dotglob off
extglob on
failglob off
globstar off
globasciiranges off
nocaseglob off
nullglob off
Here's my version information:
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Echo $- outputs
himBH
The correct syntax is:
#!/bin/bash
shopt -s nullglob
for image in $HOME/Desktop/Im*.jpg; do
convert "$image" ...
done
Another option may be to use the mogrify
command which is part of the ImageMagick suite:
# Create an output directory for cropped images
mkdir cropped
# Crop image and direct output to sub-directory
mogrify -gravity center -crop 95x95% -path cropped *.jpg
You should definitely not parse the output of ls
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments