To create a shell script to do something in a directory, open a text editor and type below code.
for d in $(find /root/ewrim/images -maxdepth 10 -type d)
do
....
done
Optimize images via Linux Terminal
If you want to optimize images in the terminal as bulk you need to jpegoptim command. First, you should install jpegoptim.
$ apt-get install jpegoptim
$ jpegoptim my_photo.jpg
The output should be like below.
my_photo.jpg 600x600 24bit N JFIF [OK] 6975 --> 3564 bytes (54.66%)
You can use *.jpg for all jpeg files. You can also use compression quality. –max= (0-100)
$ jpegoptim --max=20 my_photo.jpg
If you want to optimize all jpeg files you can use *.jpg.
Resize Images via Linux Terminal
You can resize image via imagemagick.
$ apt-get install imagemagick
$ convert *.jpg -resize 300x300\> *.jpg
The first *.jpg means convert all jpeg files. -resize 300×300 defines the new scale. The last *.jpg means overwrite files. If you don’t want to overwrite you can use *_compressed.jpg.
Create a shell script for optimizing and resizing images
Open a text editor and type below codes.
jpegoptim --max=10 *.jpg
convert *.jpg -resize 300x300\> *.jpg
Then save it as optimize.sh . After then, type below command on the terminal for making it executable.
chmod +x optimize.sh
Now you can use below command to run your codes.
./optimize.sh
Create a shell script for optimizing and resizing images in All Sub-folders
To resize and optimize all .jpg images in all sub-folders you can use below script.
#get sub-directories
for dir in $(find /root/ewrim/images -maxdepth 10 -type d)
do
#change directory
cd $dir
#show active directory - it's not mandatory. But I like to see directory name which is processing
pwd
#resize images
convert *.jpg -resize 300x300\> *.jpg
#optimize images
jpegoptim --max=10 *.jpg
#go back
cd ..
done
This post aims to teach imagemagick and jpegoptim. You can use the below code for optimization instead of jpegoptim. You can give 0-100 for quality.
convert *.jpg -quality 10 .jpg
Be First to Comment