Batch resising for web
Sun 05 October 2025
Smaller environment (Photo credit: wikipedia)
I recently tried to measure the environmental impact of this website. I used an online tool. The result shows the easiest way to improve my score was to reduce image sizes. This could easily be done with ImageMagick.
I wrote a small bach script to resize my files:
#!/bin/bash
# resize images for web use
target_size=200 # in kb
output_dir="web"
mkdir -p $output_dir
resize_image() {
echo "image $1..."
output_file=$output_dir/$1
cp "$1" "$output_file"
current_size=$(( $(stat -c%s "$output_file") / 1024 ))
factor=95
while (( current_size > target_size ))
do
echo "current size: $current_size"
current_size=$(( $(stat -c%s "$output_file") / 1024 ))
magick "$1" -resize $factor% "$output_file"
factor=$(( factor - 5 ))
done
}
for image in *jpg
do
resize_image "$image"
done
It tries several resize factor (while loop, factor being redefined line 20) until the output size is lower than a predefined threshold.
Then, I needed to replace path in all my articles.