How to Automate PNG & JPG Image Optimization in Windows
August 20th, 2012 by Chris Olberding
Even when using the best settings when “Saving to Web” in Photoshop there is usually still a fair amount of compression that can be accomplished without reducing image quality by using any number of free tools such as JpegTran and PngOut. However, most of these lack a graphic interface and require you to use the command prompt, or, as I do here, write a script that applies some of the best image compression algorithms to any number of png and jpg images within a directory.
Applying JpegTran and PNGOut via context menu
The script adds an option to the context menu in Windows when you right click a directory. Running the script on a directory losslessly compresses and replaces any PNGs and JPEGs inside it using JpegTran and PngOut.
- Download both jpegtran.exe and pngout.exe. You can use the version in the zip file on this post or replace them with newer versions
- Put the files in
C:/Program Files/Image Optimization.
- Use Notepad to create a simple script file called
optimize.bat,and place it inside of
C:/Program Files/Image Optimization.
- Copy and paste the following into
for %%i in (*.jpg) do "C:\Program Files\Image Optimization\jpegtran.exe" -optimize -progressive -copy none "%%i" "%~1\OptimizedJPEGS\%%i"
move /Y "%~1\OptimizedJPEGS\*.*" "%~1"
for %%i in (*.png) do "C:\Program Files\Image Optimization\pngout.exe" "%%i"
- Now to add the option to the context menu. Open the registry editor (regedit).
- Navigate to
HKEY_CLASSES_ROOT\Directory\shell\in the registry.
- Right click “Shell” and select
New -> Key.Enter a name for the command that will appear in the context menu, I’ve named mine “Optimize Images”.
- Create another key called
commandunder the key you just created.
- Double click the key within
(Default),and then add the following Value Data:
"C:\Program Files\Image Optimization\optimize.bat" "%1"
- Test it out by right clicking and applying the script on a folder with at least one jpg or png.
Breaking down the script
The most common issue you’ll run into is likely incorrect paths in the script. If the files are write protected the script will fail, not optimizing PNGs in the directory and leaving the optimized JPGs in a sub-directory called “OptimizedJPEGS”. Let’s take another look at the script.
REM Turn off displaying actual commands @echo none REM Move to target directory cd %1 REM Create temporary directory for JPEGS md "%~1\OptimizedJPEGS" REM Cycle through .jpg files and run JpegTran with the optimize, progressive, and copy none options for %%i in (*.jpg) do "C:\Program Files\Image Optimization\jpegtran.exe" -optimize -progressive -copy none "%%i" "%~1\OptimizedJPEGS\%%i" REM Force overwrite of files from temp directory to target directory move /Y "%~1\OptimizedJPEGS\*.*" "%~1" REM Remove temporary directory rd "%~1\OptimizedJPEGS" REM Cycle through .png files and run PngOut with default settings overwriting originals for %%i in (*.png) do "C:\Program Files\Image Optimization\pngout.exe" "%%i" REM add in pause here to keep the command prompt open if you're debugging pause
@echo none: hides the commands being executed. I suggest removing this line if you need to look closely at why the command is erroring out.
%1: This is the path to the directory you a running the script on.
\OptimizedJPEGS: JpegTran does not like overwriting files. I get around this by creating a directory to output the optimized files into, I then move the files to the original directory and delete the temporary directory. If the jpgs are write protected, the move will fail, leaving the optimized files in the OptimizedJPEGS directory.
- Add the
-verboseoption if you’re having problems with JpegTran.
Room for Improvement
Right now, the script is not recursive. It will only process the images in the root of the directory that you run the script on. However, it shouldn’t be a huge project to modify the script to make it go through sub-directories.
We work on PCs here, but you should be able to adapt the idea in this post to a Mac environment via shell scripts. I’d be interested in any attempt to port this idea.
Download Zip of the required files
You can skip a few steps by downloading this zip file that includes the two image compression programs and the optimize.bat files already created. There are modified directions included as well–enjoy.