Ok, this is a second tutorial/tip for those of you who work on Linux and want to keep things neat…
Sometimes, when I work with other people for diferent design projects (mainly 3D animation) I get lots of folders with textures for the 3D assets and scenes we're working with… Almost always, those texture images are PNGs or JPGs and I can't convert them to other better formats (like WebP) because I would mess up the 3D files, which are already set up to look for PNG or JPG files.
In those cases I do what I'm about to share here… I optimize all the image files. This is a "lossless" process (files don't lose visual quality) and you can still get small savings in file sizes. We're talking an average of 10% or 20% at most; but when we're talking about several hundreds of MB and sometimes even GBs, 10% or 20% can make a noticeable difference, especially when you have to download or upload those files across multiple computers.
So let's begin… Unlike the previous tutorial, this time we will need 2 programs for this to work (both of them free and open source). One of them is called "oxipng" and the other one is called "jpegoptim".
To install these apps, look on your distro's repositories and search how to install them. For my distro (Arch based) I install them with:
sudo pacman -S oxipng jpegoptim
Ok, now we're ready! Just like with the previous tutorial, this script will automatically optimize all JPG and PNG images inside a folder. Normally, I just replace all files automatically, but the version of the script I'm sharing here creates a new folder called "optimized" and makes a copy of all the images that are going to be optimized. The original, unoptimized images outside won't be touched.
Let's suppose the folder you want to optimize is in
"/home/user/pictures/"
, the script will look like this:input_folder="/home/user/Pictures/" ; cd "$input_folder" && mkdir -p optimized && cp -u "$input_folder"/* "$input_folder"/optimized/ 2>/dev/null ; cd optimized && find -type f -iname "*.png" | xargs -P2 -I{} oxipng -r -o 5 -i 0 -a --nb {} ; find -type f -iname "*.jp*g" | xargs -P8 -I{} jpegoptim -s {}
This is like the previous tutorial I wrote ( #1015187 ), remember, the only important thing you need to change is the first part, which is the folder you want to optimize… If the folder you want to optimize is something like
"/home/user/work/textures/"
, then the script will begin with:input_folder="/home/user/work/textures/"
And the complete script now would look like this:
input_folder="/home/user/work/textures/" ; cd "$input_folder" && mkdir -p optimized && cp -u "$input_folder"/* "$input_folder"/optimized/ 2>/dev/null ; cd optimized && find -type f -iname "*.png" | xargs -P2 -I{} oxipng -r -o 5 -i 0 -a --nb {} ; find -type f -iname "*.jp*g" | xargs -P8 -I{} jpegoptim -s {}
Again, just like with the previous tutorial, I did a test on one folder with a bunch of PNGs and JPGs inside, the folder was 756 MB in size, after running the script, the "optimized" sub-folder with all the optimized images was 636 MB, that's around a 16% size reduction. Not bad considering this method is visually lossless and keeps the original formats of the images (PNG or JPG).
One final word (that I forgot to mention in the previous tutorial... Ill add it as a comment in a moment)… Just like you help me with your awesome zaps, consider donating to all these fantastic free and open source projects. Here are the links to their repositories:
oxipng:
https://github.com/oxipng/oxipng
jpegoptim:
https://github.com/tjko/jpegoptim
Feel free to comment or ask anything!