Responsive Ads Here

Tuesday 9 July 2013

How to Automate PNG & JPG Image Optimization in Windows

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.
  1. 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
  2. Put the files in C:/Program Files/Image Optimization.
  3. Use Notepad to create a simple script file called optimize.bat, and place it inside of C:/Program Files/Image Optimization.
  4. Copy and paste the following into optimize.bat:
    @echo none
    cd %1
    md "%~1\OptimizedJPEGS"
    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"
    rd "%~1\OptimizedJPEGS"
    for %%i in (*.png) do "C:\Program Files\Image Optimization\pngout.exe" "%%i"
  5. Now to add the option to the context menu. Open the registry editor (regedit).
  6. Navigate to HKEY_CLASSES_ROOT\Directory\shell\ in the registry.
  7. 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”.
  8. Create another key called command under the key you just created.
  9. Double click the key within command named (Default), and then add the following Value Data:"C:\Program Files\Image Optimization\optimize.bat" "%1"
  10. 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 -verbose option 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.

No comments:

Post a Comment