Batch Optimize JPEG Images using jpegtran

Since couple of days we had worked to optimize our page load. The first step which we took was to optimize existing images by using few best tools which improves loading of any web page as image size gets reduced.

For PNG images we used PNG optimizer which supports batch update but for JPEG images it was a bit of trouble. One option was to convert all of them into png  though it might cost a little extra bandwidth ( Why ? Read this ), but that will take editing of lot of posts etc. So we thought why not optimize the JPEG files itself.

Checking on Smush It @ Yahoo I found this tool used jpegtran to optimize JPEG images. So I got that downloaded only to find that there is no support for batch update. But I was lucky to find a script which you can use it on windows.

@echo off
cd %1
for %%i in (*.jpg) do “C:UsersAshishDesktopjpegtran.exe” “-progressive” “%%i” “C:UsersAshishDesktopProgressiveJPEG%%i”

  • %1 =  Folder path you will drop on the bat file.
  • %%i is the file found and it is checked against jpg extension.
  • You can pass few options / switch to jpegtran utility which is marked in bold. Make sure to keep it in quotes and separated. Test different options as per your need, I found using different options bring about 5 to 10 MB difference.
Jpegtran Options
Jpegtran Options

Create a “.bat” file out of this and drop the folder which has all JPEG files. You can use any of the switch /options by replacing the text above as explained. Thats it, This will not only optimize your jpeg files but will do it in batch.

This solution fits best if you have situation like me where it is not possible to update and replace all images but from now on try using PNG for images. However if there is a need to use JPEG and if you are using WordPress, I would suggest to use the SmushIt wordpress plugin but it works only if your images are located under WordPress Folder. Its hardcoded into the plugin.

1 COMMENT

Leave a Reply