Optimizing JPEG graphics files with ImageMagick
Created:17 Mar 2017 13:36:30 , in Host development, Web development
A script for optimizing JPGs with ImageMagick below.
The script
#!/usr/bin/env bash
# script : optimize_jpegs.sh
# description : optimize JPEG images in place
# prerequisites : ImageMagick program suite installed on the system
# Author : Sylwester Wojnowski
# WWW : wojnowski.net.pl
# Usage : optimize_jpegs.sh directory [quality]
# directory - path to an existing directory on the system where images in JPEG format are kept
# quality - percentage in range of 1 - 99, 70% by default
# Example : optimize_jpegs.sh /tmp/my/jpegs 65%
optimize_jpegs(){
printf "Beginning JPEG files optimization ...\n"
local source_dir=${1%/}
local quality=${2:-"70%"}
local count=0
local space_total_raw_before=$(du -hs "$source_dir")
local space_total_before="${space_total_raw_before/%[[:blank:]]${source_dir}}"
local space_total_raw_after=
local space_total_after=
[[ -z $( which mogrify ) ]] && {
printf "ImageMagick not installed on the system. Exiting ...\n";
exit 1;
}
[[ ! -d "$source_dir" ]] && {
printf "$source_dir is not a directory on the system. Exiting ...\n";
exit 1;
}
local p_reg='^([1-9]|[1-9][0-9])%$';
[[ ! $quality =~ $p_reg ]] && {
printf "Quality parameter not in range. The correct range is 1 - 99 %. Exiting ...\n"
exit 1;
}
shopt -s extglob;
while read -r -d ''; do
mogrify -strip -interlace Plane -quality "$quality" -define jpeg:dct-method=float -sampling-factor 4:2:0 "$REPLY"
(( count++ ))
printf '.'
done < <( find "$source_dir"/*\.jp?(e)g -print0 )
printf '\n';
shopt -u extglob
space_total_raw_after=$(du -hs "$source_dir")
space_total_after="${space_total_raw_after/%[[:blank:]]${source_dir}}";
printf "Optimization complete!\n"
printf " $count JPEG files optimized and saved to: $source_dir.\n"
printf " Total space taken before optimization: %s, Space taken after: %s\n" "${space_total_before}" "${space_total_after}"
}
optimize_jpegs "$1" "$2"
Run it
Optimizing JPEG files in /tmp/my/jpegs to 70% quality ( default ).
optimize_jpegs.sh /tmp/my/jpegs
Optimizing JPEG files in /tmp/my/jpegs to 60% quality.
optimize_jpegs.sh /tmp/my/jpegs 60%
This post was updated on 06 Oct 2021 21:23:28
Tags: BASH , image , ImageMagick
Author, Copyright and citation
Author
Author of the this article - Sylwester Wojnowski - is a sWWW web developer. He has been writing computer code for the websites and web applications since 1998.
Copyrights
©Copyright, 2024 Sylwester Wojnowski. This article may not be reproduced or published as a whole or in parts without permission from the author. If you share it, please give author credit and do not remove embedded links.
Computer code, if present in the article, is excluded from the above and licensed under GPLv3.
Citation
Cite this article as:
Wojnowski, Sylwester. "Optimizing JPEG graphics files with ImageMagick." From sWWW - Code For The Web . https://swww.com.pl//main/index/optimizing-jpeg-graphics-files-with-imagemagick
Add Comment