1 min read

Batch Resize and Optimize Images

My routine workflow for handling images involves resizing them to various dimensions and optimizing their file sizes.

Here’s how I process images in batch using ImageMagick and a Docker container:

# Install ImageMagick if you don’t have it
sudo apt install imagemagick
 
cd /path/to/your/folder
 
# Rename files sequentially
a=1
for i in *.jpg; do
  mv "$i" "$a.jpg"
  let a=a+1
done
 
# Resize images to a maximum width of 1200px while maintaining the aspect ratio
mogrify -format jpg -resize "1200>" *.*
 
# Resize to 300px width if needed
mkdir 300
cp *.jpg 300/
cd 300
mogrify -format jpg -resize "300>" *.*
mv *.jpg ../
rm -rf 300
 
# Finally, optimize the images
cd /path/to/your/folder
docker run --rm -it -v $(pwd):/src funbox/optimizt .
 

In the end, your files will be structured like this:

ls
1.jpg
1-300.jpg
2.jpg
2-300.jpg
# ...and so on

This workflow ensures that your images are properly resized and optimized for various use cases.

Related: Batch Rename Files in Ubuntu & Windows