Optimize multiple files with jpegtran
jpegopt is a small shell script made to enable use of the libjpeg jpegtran utility recursively—to optimize mulitiple JPEG files, including those located in sub-directories—rather than have to laboriously enter jpegtran commands for each file to be optimzed.
jpegopt was built and tested using jpegtran version 8d and the Bourne shell, on Mac OS X. It may work with other jpegran versions, in other shells, and on other unix-like systems – though this is untested.
This script was a learning exercise, inspired by, and owing much to these answers on stackoverflow. This was also my first attempt at shell scripting, so much reference was made to this Shell Scripting Primer and this Advanced Bash-Scripting Guide. It is almost certainly overkill, but I enjoyed making it and intend to put it to good use.
Navigate to your preferred directory and clone the repository from git:
cd path-to-directory git clone https://github.com/tomchkk/jpegopt.git jpegopt
Navigate to the newly cloned repository and modify the file permissions:
cd jpegopt sudo chmod 755 jpegopt.sh
The script can then be run from the cloned repository:
Or, by copying the file to
/usr/local/bin, it can be run from anywhere (assuming
/usr/local/bin is in your
sudo cp jpegopt.sh /usr/local/bin/jpegopt.sh jpegopt.sh -v
To print help menus for both jpegopt and jpegtran, enter the following at the command prompt:
jpegopt accepts multiple arguments— in no particular order—though by convention they are listed as follows:
directory– the directory in which to search for JPEG files. If omitted, the script will default to the current working directory.
options– jpegopt's own options.
jpegtran:switches– valid jpegtran switches.
As well as accepting all of jpegtran's switches, jpegopt also has it's own options:
-maxdepthsets the maximum directory depth at which to search for JPEG files.
-maxdepthdefaults to 1, which would limit the search to the directory given as the directory argument (or the current working directory, if the argument is omitted). This option passed as
-maxdepth 2would search the given directory and any immediate child directories, and so on...
-overwritesets the method by which jpegopt handles any original files to be optimized. The default setting,
-overwrite bk, takes a back-up of the original file and replaces it with the optimized file. Passing this option as
-overwrite dxwill destructively replace the orginal file with the optimized one.
-overwrite offdisables this feature altogether, resulting in two files: original.jpg and original.jpg.optmzd.
-dryrunenables the dry-run mode, giving a list of files found that would be optimized, according to the given options.
-debugenables a debug mode, printing the jpegtran command that would be used, according to the given options.
Since the purpose of jpegopt is to optimize JPEG files, certain of jpegtran's switches are enabled by default, as detailed below. All other valid jpegtran switches can be passed as arguments to jpegopt.
-copy– the value of this switch defaults to
none. This value can be over-ridden with any other valid value. The switch can be disabled by passing
-copy offas an argument to jpegopt.
-optimize– this switch is enabled by default, but can be disabled by passing
-optimize offas an argument to jpegopt.
N.B. jpegtran already handles non-existent switches and invalid argument values gracefully, so jpegopt is purposefully unaware of all of jpegtran's available switches.
From the cloned source directory, jpegopt can be tested using the following command:
This was a weekend(ish) project. Feel free to make use of the utility or the source as you wish, but you do so entirely at your own risk.