1
votes

I would like to iterate this code over a directory of files:

convert Film_Crew.jpg -resize 1920x1080 -size 1920x1080 xc:black +swap -gravity center -composite Film_Crew_resize.jpg

Images in my directory:

Film_Crew.jpg

Film_Crew copy.jpg

Film_Crew copy 2.jpg

Film_Crew copy 3.jpg

Film_Crew copy 4.jpg

Film_Crew copy 5.jpg

Film_Crew copy 6.jpg

I tried to use this code because it worked for me last time I tried, but it isn't working now.

for i in *.*; do convert $i -auto-orient -resize 1920x1080 -size 1920x1080 xc:black +swap -gravity center -composite "new/${i%.*}.jpg";done

I am on a Mac working in Terminal. The error I get is this:

convert: unable to open image Film_Crew': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format' @ error/constitute.c/ReadImage/504. convert: unable to open image copy': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format' @ error/constitute.c/ReadImage/504. convert: unable to open image 2.jpg': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no such imageblack' @ error/mogrify.c/MogrifyImageList/8770. convert: no images defined new/Film_Crew copy 2.jpg' @ error/convert.c/ConvertImageCommand/3258. convert: unable to open imageFilm_Crew': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format ' @ error/constitute.c/ReadImage/504. convert: unable to open imagecopy': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format ' @ error/constitute.c/ReadImage/504. convert: unable to open image3.jpg': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no such image black' @ error/mogrify.c/MogrifyImageList/8770. convert: no images definednew/Film_Crew copy 3.jpg' @ error/convert.c/ConvertImageCommand/3258. convert: unable to open image Film_Crew': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format' @ error/constitute.c/ReadImage/504. convert: unable to open image copy': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format' @ error/constitute.c/ReadImage/504. convert: unable to open image 4.jpg': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no such imageblack' @ error/mogrify.c/MogrifyImageList/8770. convert: no images defined new/Film_Crew copy 4.jpg' @ error/convert.c/ConvertImageCommand/3258. convert: unable to open imageFilm_Crew': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format ' @ error/constitute.c/ReadImage/504. convert: unable to open imagecopy': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format ' @ error/constitute.c/ReadImage/504. convert: unable to open image5.jpg': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no such image black' @ error/mogrify.c/MogrifyImageList/8770. convert: no images definednew/Film_Crew copy 5.jpg' @ error/convert.c/ConvertImageCommand/3258. convert: unable to open image Film_Crew': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format' @ error/constitute.c/ReadImage/504. convert: unable to open image copy': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format' @ error/constitute.c/ReadImage/504. convert: unable to open image 6.jpg': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no such imageblack' @ error/mogrify.c/MogrifyImageList/8770. convert: no images defined new/Film_Crew copy 6.jpg' @ error/convert.c/ConvertImageCommand/3258. convert: unable to open imageFilm_Crew': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no decode delegate for this image format ' @ error/constitute.c/ReadImage/504. convert: unable to open imagecopy.jpg': No such file or directory @ error/blob.c/OpenBlob/2701. convert: no such image black' @ error/mogrify.c/MogrifyImageList/8770. convert: no images definednew/Film_Crew copy.jpg' @ error/convert.c/ConvertImageCommand/3258. convert: unable to open image `new/Film_Crew.jpg': No such file or directory @ error/blob.c/OpenBlob/2701.

2
Your command works fine for me in ImageMagick 6.9.9.3 Q16 Mac OSX Sierra. What is your version of ImageMagick? If IM 7, then change convert to magick. Are you sure your file, Film_Crew.jpg exists in the location you expect? Is it corrupt? You can also do the same for a given directory using mogrify and -draw and the appropriate compose method. See imagemagick.org/Usage/basics/#mogrify_composefmw42
How do I check which version of ImageMagick I have? edit: Found it. I am on ImageMagick 6.9.7-7 Q16. I am sure Film_Crew.jpg exists in the folder I am in and it is not corrupt. I will look into mogrify documentation. Thanks.Evan Mann
Try convert -version or magick -version. If that fails, then type -a convert or type -a magickfmw42
You have files with spaces in the name, such as, new/Film_Crew copy 2.jpg. The filenames will be truncated at the first space in your wild card search and list. Thus ImageMagick will not find that file, since it is missing the suffix, etc. Put double quotes about your filename commands. Or remove those extra copies that exist in that directory.fmw42
That's true! Thanks. Do I need to add double quotes around something in my code to fix that?Evan Mann

2 Answers

1
votes

Life would be easier with mogrify - no for loop, no quoting:

mkdir new
mogrify -path new -resize 1920x1080 -background black -gravity center -extent 1920x1080 *jpg
0
votes

Try this using ImageMagick assuming all your input images are JPG. I set the Internal Field Specifier to a new line, so that spaces in the filenames are considered part of the name.

cd
cd path2/your_image_directory
OLDIFS=$IFS
IFS=$'\n'
list=`ls *.jpg`
for img in $list; do 
convert \( "$img" -auto-orient -resize 1920x1080 \) \( -size 1920x1080 xc:black \) +swap -gravity center -composite "./new/${img%.*}.jpg"
done
IFS=$OLDIFS