Batch image resizing with Imagemagick

in #bash6 years ago (edited)


This tutorial shows you how to list your images in a csv file and resize every image in that list automatically.
WHOOO! AUTOMATION! 🎉


Imagemagick

ImageMagick is a free and open-source software suite for displaying, converting, and editing raster image and vector image files. 

Prepare the scripts

  • Log in your Linux system
  • Run the following command in your bash console to list every JPG in the specified folder recursively and save it into a csv file
find /home/project/ -name "*.jpg" -print| while read LINE; do echo "$LINE" ; done > list_with_images.csv
  • Create a file named "resize_with_imagemagic.sh"
# define the settings
export MAGICK_THREAD_LIMIT=1;
export MAGICK_THROTTLE_LIMIT=100;
export MAGICK_MEMORY_LIMIT=500mb;

start=0;
end=$(cat $1 | wc -l)

loop through every line of the given file

while read file; do
echo "[x] Optimizing $file"
echo $(($end-$((++start)))) elements remaining of $end
replace="_optimized.jpg"
targetFile="${file/.jpg/_optimized.jpg}"
echo "Optimizing $file to $targetFile"
convert "$file" -resize 1920 "$targetFile"
# let the CPU rest for 10 sec
sleep 10
done < $1

You can edit the script to your liking. it doesn't replace the original image, but creates a new one with a postfix of "_optimized".
Original: test_image.jpg
Optimized: test_image_optimized.jpg

Run the script

sh resize_with_imagemagic.sh list_with_images.csv

Learn more about Linux bash tricks



Posted from my blog with SteemPress : http://mpawirodinomo.abovenormal.co/2019/05/batch-image-resizing-with-imagemagick/
Sort:  

As a follower of @followforupvotes this post has been randomly selected and upvoted! Enjoy your upvote and have a great day!

Looking for some fun games to play on Steemit? Try your luck with Magicdice or Drugwars

Thank you, this tutorial is very helpful, for more attention you cold add the tag #opensource and #tutorial next time.

Posted using Partiko Android

I will, thank you!

Coin Marketplace

STEEM 0.26
TRX 0.20
JST 0.039
BTC 95654.77
ETH 3579.91
USDT 1.00
SBD 3.84