JPG and PNG image optimization

Jpegoptim and Pngcrush are two command-line tools to optimize JPG and PNG image files.

Jpegoptim is a tool which can lossless optimize JPG images, which means it can reduce their file size without affecting their visual quality. It works by analyzing the image and adjusting its compression parameters to achieve the best possible compression ratio, while still maintaining the same level of image quality.

Pngcrush is a tool that can optimize PNG images by reducing their file size without affecting their visual quality. It works by removing unnecessary data from the image file, such as metadata and color information, that is not used in the image. It can also convert the image to a different color format to reduce its file size further.

Both tools can be helpful for websites or applications which deal with a lot of images, as it can reduce the overall loading time. In addition, both tools remove any EXIF information and prepare the images for a possible upload to the Internet without revealing unnecessary private information. However, it should be noted that this process also leads to a change in the creation and modification dates.

Batch scripts

The following Windows batch scripts will use the respective exe file and loop for the root and all subfolders to optimize the corresponding images. Please note that the original images, which are found in the directory, will be overwritten. Remember to create a backup of the original files.

Jpegoptim

@echo off

for /r %%f in (*.jpg) do jpegoptim.exe --preserve --strip-all --all-progressive --force %%f

@echo off turns off the display of the commands being executed in the console window. This is purely for cosmetic reasons and doesn’t affect the functionality of the script.

for /r %%f in (*.jpg) sets up a for loop that iterates over all files with the *.jpg extension in the current directory and all subdirectories. The %%f variable represents each file in the loop.

do jpegoptim.exe --preserve --strip-all --all-progressive --force %%f specifies the action to be taken on each file in the loop. This command calls the jpegoptim.exe tool and applies several optimization options to each file. Specifically, it preserves the file’s modification timestamp with the --preserve option, removes all metadata and comments with the --strip-all option, applies progressive JPEG encoding with the --all-progressive option, and forces the file to be overwritten with the --force option.

Jpegoptim CMD Output
JPG optimization, -12 % in file size

Pngcrush

@echo off

for /r %%f in (*.png) do pngcrush.exe -rem alla -rem text -reduce -brute -ow "%%f"

do pngcrush.exe -rem alla -rem text -reduce -brute -ow "%%f specifies the action to be taken on each file in the loop. This command calls the pngcrush.exe tool and applies several optimization options to each file. Specifically, it removes all ancillary chunks with the -rem alla option, removes all textual information with the -rem text option, applies various compression and filtering techniques with the -reduce and -brute options, and overwrites the file with the -ow option.

Pngcrush CMD Output
PNG optimization, -41 % in file size
asterix Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *