Optimize images for the web in bulk

Date posted: 2014-01-12
Last updated: 2026-10-03

Lossless compress and optimize hundreds of PNG images for the web in bulk, using OptiPNG on the Windows command line.

Optimizing and lossless compressing images is one of those items that always pops up when analyzing your website with Google PageSpeed Insights. Images have to be lossless compressed to save many bytes of data and thus bandwidth. This speeds up the download time of your website. But, how do we optimize images for the web? Or, what if I have hundreds of images that need optimization? Let's optimize and lossless compress images in bulk using OptiPNG, read on.

OptiPNG lossless compresses PNG images without any installation: one FOR /F loop on the Windows command line runs it on every PNG file in a directory tree. For JPEG images, use jpegtran or jpegoptim in the same way.

I originally wrote this post in 2014, when Windows 7 was common and PNG and JPEG were the formats to optimize. The command line approach still works, but nowadays you should also consider modern image formats like WebP and AVIF.

Optimize images for the web in bulk with OptiPNG

In this post we optimize PNG images as an example. The same goes for lossless compressing and optimizing JPEG images, you only need another tool (jpegtran or jpegoptim). Google PageSpeed Insights already mentions the tools, we only have to download and use them.

So, go ahead and download OptiPNG. There is no installation required, it's a run once executable for the command line.

If you are familiar with the command line, you can very easily execute OptiPNG with its default settings on all your PNG files in a directory:

D:\Dev\sites> FOR /F "delims=" %I in ('dir /b/s *.png') do @c:\optipng-0.7.4-win32\optipng.exe "%I"

This lossless compresses and optimizes all PNG images in bulk, found within d:\dev\sites\ and recursively in its sub directories, making your images and website footprint smaller, resulting in a faster download of your website.

OptiPNG: lossless compressing images on the fly

The following command lossless compresses all PNG files recursively, from within my content directory, using optimization level 5 (-o5):

FOR /F "delims=" %I in ('dir /b/s *.png') DO @C:\Temp\optipng-0.7.5-win32\optipng.exe -o5 "%I"

The "delims=" option and the quotes around %I make sure file names with spaces are processed correctly. In a batch file, use %%I instead of %I.

Find more website optimization tips in minify JavaScript, CSS and compress images.

I write these posts in my spare time, based on real problems from my day job as a sysadmin. If this one saved you some debugging time, a small donation is much appreciated. Thanks! 🙏

Leave a Comment