Optimize multiple files with jpegtran
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
tests
LICENSE
README.md
jpegopt.sh

README.md

JPEGOPT

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.

Installation

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:

./jpegopt.sh -v

Or, by copying the file to /usr/local/bin, it can be run from anywhere (assuming /usr/local/bin is in your $PATH):

sudo cp jpegopt.sh /usr/local/bin/jpegopt.sh
jpegopt.sh -v

Usage

  • jpegopt Help

    To print help menus for both jpegopt and jpegtran, enter the following at the command prompt:

      jpegopt.sh -h
    
  • jpegopt Arguments

    jpegopt accepts multiple arguments— in no particular order—though by convention they are listed as follows: directory, options and jpegtran:switches.

    • 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.

  • jpegopt Options

    As well as accepting all of jpegtran's switches, jpegopt also has it's own options:

    • -maxdepth sets the maximum directory depth at which to search for JPEG files. -maxdepth defaults 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 2 would search the given directory and any immediate child directories, and so on...

    • -overwrite sets 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 dx will destructively replace the orginal file with the optimized one. -overwrite off disables this feature altogether, resulting in two files: original.jpg and original.jpg.optmzd.

    • -dryrun enables the dry-run mode, giving a list of files found that would be optimized, according to the given options.

    • -debug enables a debug mode, printing the jpegtran command that would be used, according to the given options.

  • jpegtrans Switches

    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 off as an argument to jpegopt.

    • -optimize – this switch is enabled by default, but can be disabled by passing -optimize off as 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.

Testing

From the cloned source directory, jpegopt can be tested using the following command:

./tests/jpegopt.test.sh

Disclaimer

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.