I have previously written about how to resize all the images in a folder. That script was based on lossy compression of the files. However, there are also tools for optimizing image files losslessly. One approach is to use the.jpgoptim](https://github.com/tjko.jpgoptim) function available on ubuntu. Here is an excellent explanation of how it works.

Lossless optimization

As part of moving my blog to Hugo, I took the opportunity to optimize all the images in all my image folders. That was done with this one-liner:

find . -name *.jpg -exec.jpgoptim {} \;

I did not calculate the optimization level. However, glancing at the terminal output, it looks like the images have been optimized with 0-10%.

Terminal output showing JPEG optimization results with file sizes and compression percentages

Fix orientation

While at it, it may also be worth fixing the orientation of files. This can easily be done by running the autorot function:

find . *.jpg -exec jhead -autorot {} \;

It will ensure that the right header is written to the file, which is not always the case coming out of cameras.