In Imagemagick for linux how do I do a batch convert for a directory
I want to be able to specify the location in both where to scan and where the converted file will go.
It's just there is a lot of conversions and I've got a script which should sort it for me. Currently I've tried
convert -resize 300x300 > /media/usbdisk1/development/ephoto/richard/images/gallery/2007/29/normal/*.jpg /media/usbdisk1/development/ephoto/richard/images/gallery/2007/29/tn_med/$1.jpg
and
for i in $( ls /media/usbdisk1/developme开发者_如何学JAVAnt/ephoto/richard/images/gallery/2007/29/normal); do /usr/convert resize 360x360 > /media/usbdisk1/development/ephoto/richard/images/gallery/2007/29/normal/$i /media/usbdisk1/development/ephoto/richard/images/gallery/2007/29/tn_med/$i done;
for i in $( ls /media/usbdisk1/development/ephoto/richard/images/gallery/2007/29/normal); do
convert -resize 360x360 /media/usbdisk1/development/ephoto/richard/images/gallery/2007/29/normal/$i /media/usbdisk1/development/ephoto/richard/images/gallery/2007/29/tn_med/$i;
done
got it!
As comments suggested, you can use the find
command:
outdir=/media/usbdisk1/development/ephoto/richard/images/gallery/2007/29/tn_med
cd /media/usbdisk1/development/ephoto/richard/images/gallery/2007/29/normal
find . -iname '*.jpg' -print0 | xargs -I{} -0 -r convert -resize 300x300 {} $outdir/{}
By using -print0
and xarg's -0
option, this also handles filenames with spaces and other odd characters.
There's no reason to repeat your long directory three times. Use a variable for the base. And don't use ls
:
base="/media/usbdisk1/development/ephoto/richard/images/gallery/2007/29"
for file in "$base/normal/*"
do
convert -resize 360x360 "$file" "$base/tn_med/$(basename $file)"
done
Instead of basename
you could do it this way:
convert -resize 360x360 "$file" "$base/tn_med/${file##*/}"
精彩评论