How to Optimize All JPG and PNG Files in a Folder Using the Windows Command Line

Images on the internet should always be optimized where possible to reduce the load time of your websites. Of course the best solution is to use a CDN but sometimes for whatever reason like me you inherit some folders full of unoptimized images you can run the following commands to reduce the load on your server. These could also be added to batch files and run in the scheduler if required.

JPG

  1. Download [jpegtran.exe] (http://jpegclub.org/jpegtran.exe)
  2. Run the following command:
for /R "C:\yourimagesfolder" %I in (*.jpg) do @"C:\yourdownloadfolder\jpegtran.exe" -copy none -optimize -progressive -outfile %I %I

PNG

  1. Download [optipng.exe] (https://sourceforge.net/projects/optipng/files/OptiPNG/optipng-0.7.7/optipng-0.7.7-win32.zip/download)
  2. Run the following command
for /R "C:\yourimagesfolder" %I in (*.png) do @"C:\yourdownloadfolder\optipng.exe" -o7 -strip all %I
comments powered by Disqus