8

I tried to crop several images of the same size, but when I crop 5 images and the process is complete, only 4 images are cropped, 1 image is not cropped, if I crop 1 image an error will appear...

I'm using the command

convert -crop 599x500+147+200 *.jpg

screenshot batch crop 5 picture but processes 4 pictures only

screenshot batch crop 1 picture but get error

How should I adjust my command?

1 Answer 1

13

The first problem seems to happen because when convert receives a list it will use the last file specified as the output file name. All the new files get named after the last file, which itself is left untouched (this is unlikely to be what you want or expect, but it is probably better than your files being unexpectedly overwritten).

When there is only one file in the directory (so your glob expands to the one file), convert complains about lack of defined images because it expects at least one input file and an output file name to be specified. The position you are using for your glob is the output filename position, so convert is complaining about the lack of an input file in your second image.

For more reliable results you should specify the input and output files:

 convert input-file.jpg -crop 599x500+147+200 new-file.jpg

If the name is long, you should be able to use tab completion (type the first few characters and then press tab to have the shell finish the name) for both input file and output file (that is, it's working fine for me).

For batch processing, you can use the shell to run your command once on each file and use a bit of string manipulation to construct the new names, so the new files have sane names, something like:

for f in *.jpg; do 
    echo convert "$f" -crop 599x500+147+200 "${f/.jpg/-cropped.jpg}"
done

With the echo on the second line, this shows what the new names will be. If the new names look right, you can remove echo and run the commands again to actually crop the images.

Putting the new files into a new directory could be easier, especially if your filenames have different extensions...

mkdir cropped
for f in *.jpg *.png; do
    echo convert "$f" -crop 599x500+147+200 cropped/"$f" 
done

Again, you need to remove echo before this will do anything.

1
  • On one line, something like find . -maxdepth 1 -iname "*.jpg" -type f -exec convert "{}" -crop 900x340+180+50 cropped/"{}" \; Feb 16, 2023 at 4:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .