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
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"
- 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
command
under the key you just created.
- Double click the key within
command
named (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.
C:/Program Files/Image Optimization.
optimize.bat,
and place it inside of C:/Program Files/Image Optimization
.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"
HKEY_CLASSES_ROOT\Directory\shell\
in the registry.New -> Key.
Enter a name for the command that will appear in the context menu, I’ve named mine “Optimize Images”.command
under the key you just created.command
named (Default),
and then add the following Value Data:"C:\Program Files\Image Optimization\optimize.bat" "%1"
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.
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.-verbose
option if you’re having problems with JpegTran.
No comments:
Post a Comment