How to batch / multi process cropping images in the terminal?
772
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...
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:
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.
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, soconvert
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:
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:
With the
echo
on the second line, this shows what the new names will be. If the new names look right, you can removeecho
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...
Again, you need to remove
echo
before this will do anything.