Find non progressive JPEGs in your project with the command line

Posted by in Bash, Performance

The problem

In a website project I wanted to increase the performance. One step was to convert non progressive (interlaced) JPEGs into progressive JPEGs. But how could I find quickly which images are still in interlaced mode? Google dind’t helped a lot. Mostly you had to install tools to detect the compression type. Until I found a cool snippet:

grep -c -P '\xff\xc2' myimage.jpg

The output will be 1 if it is progressive and 0 if it is interlaced.

The solution: a cool bash script to list the compression type of all images

Just copy this code into a script findNonProgressiveJpeg.sh and you can call it via

bash findNonProgressiveJpeg.sh

You should locate the script in the main folder of your project.

The script lists all JPEG files in your project and also the compression state. At the end it lists a summary of how many files are not optimized yet.

Hint: If you add a # before the PROGRESSIVE output (line 15), it will list only the interlaced files. This is helpfull for large projects.

#!/bin/bash
# Find and show all files, that are not progressive

COUNT_INTERLACE=0
COUNT_TOTAL=0

for LINE in $(find . -type f -iname "*.jpg"); do
IS_PROGRESSIVE=`grep -c -P '\xff\xc2' $LINE`

if [ "$IS_PROGRESSIVE" == "0" ] ; then
echo -e "\e[1;31mINTERLACED\e[0m: ${LINE}"
COUNT_INTERLACE=$((${COUNT_INTERLACE}+1));
else
echo -e "\e[1;32mPROGRESSIVE\e[0m: ${LINE}"
fi
COUNT_TOTAL=$((${COUNT_TOTAL}+1));
done

echo -e "\e[1;31m${COUNT_INTERLACE}\e[0m out of ${COUNT_TOTAL} files are interlaced."

I hope this is helpful! 🙂