Many thanks for Michael Brinson for taking the script below and making it work recursively. Just use Michael's code below instead of my code in the original post:
@echo none
cd %1
forfiles /M *.jpg /s /c "cmd /c @\"C:\Program Files\Image Optimization\jpegtran.exe\" -optimize -progressive -copy none @file @file"
forfiles /M *.png /s /c "cmd /c @\"C:\Program Files\Image Optimization\pngout.exe\" @file"
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.
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.
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
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.
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.
Chris Olberding is a mediocre ukulele player who owns more Funko Pop figures than any grown man should. In spite of this, he has run a successful agency for the past 10 years by providing creative vision and strategic guidance to the S4 team. Chris has been recognized as one of Jacksonville Business Journal’s 40 Under 40, and S4 has been named to the Gator 100, a list of the 100 fastest-growing businesses owned or run by a UF alumni, for the last two years.