Image Compression

Here is where you can post tips and tricks to share with other users of MX. Do not ask for help in this Forum.
Message
Author
User avatar
davidy
Posts: 818
Joined: Sat Jul 03, 2021 1:59 pm

Image Compression

#1 Post by davidy »

Chris Titus has this cool script for image compression. I had ~ 178GB of pictures that I use for my wallpapers. Collected them years ago in windows.
My entire collection compressed down to 36GB! and that's without changing the aspect or size. Here it is:
Install software:
sudo apt install jpegoptim optipng imagemagick webp -y

git clone https://github.com/VirtuBox/img-optimize.git $HOME/.img-optimize

sudo ln -s $HOME/.img-optimize/optimize.sh /usr/local/bin/img-optimize

sudo chmod +x /usr/local/bin/img-optimize

SCRIPT run in terminal
[change ONLY the FOLDER= line and the max width height values.
Sorry, This is the original code with no values set. I was so excited I put my term output in instead of the original correct code itself.
It's primarily a compressor to save space. Max Width and Height are limits only.

Code: Select all

#!/bin/bash
# Dependancies
# - img-optimize - https://virtubox.github.io/img-optimize/
# - imagemagick
# - jpegoptim
# - optipng

FOLDER="/home/titus/github/website/content/images"

# max width
WIDTH=800

# max height
HEIGHT=600

#resize png or jpg to either height or width, keeps proportions using imagemagick
find ${FOLDER} -iname '*.jpg' -o -iname '*.png' -exec convert \{} -verbose -resize $WIDTHx$HEIGHT\> \{} \;
img-optimize --std --path ${FOLDER}
Last edited by davidy on Tue Feb 07, 2023 7:35 am, edited 4 times in total.
Sys76 LemurPro-mx-23.4, EliteMinis HM90-mx-21.3, Deskmini UM350-phoenixLite win10, Qnap 12tb nas, Protectli FW4C-opnsense(=゜ω゜)

zero privacy = zero security . All MX'd Up
UAP = up above people

User avatar
h3kt0r
Posts: 147
Joined: Fri Oct 08, 2021 6:27 pm

Re: Image Compression

#2 Post by h3kt0r »

Great tip, thanks a bunch for sharing !
I'm really fond of anything related to file size optimisation.
This is awesome.
Dell OptiPlex 7010 - i7-3770 (8) @ 3.9GHz - 16Gb RAM - GeForce GT 1030 - MX 21
Panasonic CF MX4 - i5-5300U vPro (4) @ 2.9GHz - 4Gb RAM - HD Graphics 5500 - MX 21
Acer Aspire One ZG5 - Atom (2) @ 1.6GHz - 1.5Gb RAM - HD Gfx 945 - LXLE & XenialPup

User avatar
MadMax
Posts: 483
Joined: Wed Aug 04, 2021 3:25 pm

Re: Image Compression

#3 Post by MadMax »

Just keep in mind that compressing jpeg will result in a slight quality loss. I'd recommend checking some images to verify that you're ok with the quality before deleting the old files.
If it ain't broke, don't fix it.
Main: MX 23 | Second: Mint 22 | HTPC: Linux Lite 7 | VM Machine: Debian 12 | Testrig: Arch/FreeBSD 14 | Work: RHEL 8

User avatar
davidy
Posts: 818
Joined: Sat Jul 03, 2021 1:59 pm

Re: Image Compression

#4 Post by davidy »

No worries. All my wallpaper is at least 1920x1080 or bigger. I started at 5000x5000 for my wallpaper collection and when I got to my actual Pictures folder, I had a poster that was over 5600 on a side so I changed it to 6000x6000 just for that folder. If the picture is too small in aspect it can be destroyed. IOW, don't run it on your MXforum avatar pics. Obviously if you set it to 800x600, every single pic will be that size or smaller. Chris Titus's entire web facing pictures are around 60MB??, and that's with years of posting images. As for quality you can't even tell it's a smaller file.
Sys76 LemurPro-mx-23.4, EliteMinis HM90-mx-21.3, Deskmini UM350-phoenixLite win10, Qnap 12tb nas, Protectli FW4C-opnsense(=゜ω゜)

zero privacy = zero security . All MX'd Up
UAP = up above people

User avatar
m_pav
Developer
Posts: 1915
Joined: Sun Aug 06, 2006 3:02 pm

Re: Image Compression

#5 Post by m_pav »

Am I missing something here? The title of this thread is about image compression, but the script you posted does not use compression to make the file size smaller. The .jpg, or jpeg image format is a lossy format and each time an image is opened and saved at the default compression ratio, more detail is lost to the point it becomes useless. Your script outputs in .png which is a lossless format so an image with high compression will consume less storage space, but it will not lose any quality, at the expense of active memory and CPU usage as compared to saving with a lower compression ratio.

Your script takes any image type that matches the .jpg or png type and down-scales the images using imagemagick with an optimisation filter to improve the look of lower res images, but at 800x600, it's likely to be tossing out original pixels by the megaton. Not so critical if you're starting with a 1024x786 (0.75MP) image, but, taking a 3.6MP image with a 3600000 pixel count through this script, it will become a 0.48 MegaPixel image with 480,000 pixels, or roughly (92.5 %) less pixels than it used to have. That's a catastrophic loss from which there is no possible recovery if the original image is overwritten and no amount of optimisation can bring back the detail that was lost.

So, as I started my reply, and without any malice, am I missing something here? would it be wise to include a cautionary or explanatory note to the unwary?
Mike P

Regd Linux User #472293
(Daily) Lenovo T560, i7-6600U, 16GB, 2.0TB SSD, MX_ahs
(ManCave) AMD Ryzen 5 5600G, 32G, 8TB mixed, MX_ahs
(Spare)2017 Macbook Air 7,2, 8GB, 256GB SSD, MX_ahs

User avatar
wdscharff
Posts: 1162
Joined: Mon Feb 24, 2020 1:07 am

Re: Image Compression

#6 Post by wdscharff »

This programme is nothing more than a batch to convert images to a certain size. The fact that an 800x600 image requires less space than a 6000x4000 image needs no explanation.

The trick is to reduce the file size without changing the dimensions and without any noticeable loss of quality.
With JPG, you quickly come up against system-related limits.
PNG is lossless, but doesn't even come close in terms of file size.

Experience shows that the most space is saved by deleting RAWs and/or JPGs that you will never use (and there are quite a few).

With appropriate processing (which cannot be realised in a batch), you can also reduce the file size of JPGs without loss, or even with a qualitative gain. I only mention keywords such as denoising and smoothing out tonal value breaks.

An uncompressed Sony ARW (alpha7III) of 47MB+20MP JPG (superfine 98%) becomes a perfectly processed 5MB JPG (still 4000*6000px) with which you can print posters the size of a square metre. And because you don't need it anymore, you throw away the ARW and the superfine JPG and keep the most pleasing processing, bang 60MB saved on one image.

Compressing JPGs with as little loss as possible used to be an "art" that website/blog operators practised to excess because the data transfer rates of analogue modems and later ISDN were not really great.
I live in the country, until 2012 there was only DSL Light, download rate 384 Kbit/s, which is a far cry from the vdsl 50 I had in the company since 2015.

No JPG compressor introduced since 2000 lived up to its promises. NONE! But there were a few formats that were interesting, but never caught on in the market and at some point it became uninteresting, the compression rates/codecs for videos, as they represent the main load of data transmission in the WWW, became more important than any image compression.

ps: some of my UHD+WQHD and UHD only wallpapers also have only 500-800kb, but I do such tasks as denoising and smoothing out tonal value breaks in the software I use on the side.
my working horse Desktop AMD Ryzen 9 3900x, 32GB Ram // SSD ... enough
mx-fluxbox, what else?

In nature there are neither rewards nor punishments.
There are consequences.


my wallpaper gallery

User avatar
davidy
Posts: 818
Joined: Sat Jul 03, 2021 1:59 pm

Re: Image Compression

#7 Post by davidy »

I tested one of my folders before I committed. Started with jpg's and ended up with the exact same ones only using much less hd space. This script is not for your $5000 camera photo shoot shots. It's for those who have web pages (with pics of course) and simply want to save space, incl. processing and loading times. Perfect for my 135K+ wallpaper collection. If you have a few hundred or even a few thousands of pics it may not be for you as that amount doesn't take up much space to begin with. But when you start out with almost 180GB of pc wallpaper art, this script is ideal, to say the least. Xnview loads the folders noticeably faster now and the almost 150gb of space saving on my 1tb data disc is quite noice too.
If I was a pro photographer or actually took pics on a very expensive camera and planned on framing them, obviously you don't use any compression other than what is required. I've tried file compression software before but this is the first time it ever actually made any sense. Chris Titus Tech is a friend to the entire pc community, windows incl.
That poster I have of Yosemite from 1950 that's 4597x5602 px is still frameable if I were to ever print it professionally and it's only 10.2mb in size now. Free to download if you know where all the free online collections are.
What you will notice is that if you do any post processing such as cropping or whatever and then save it, you will lose compression. Not sure if you lose all but you do lose some. Like I said though. I just did some pc wallpaper, which I have boatloads of, so a lost pic here or there means nothing to me. I have 4 copies.
Sys76 LemurPro-mx-23.4, EliteMinis HM90-mx-21.3, Deskmini UM350-phoenixLite win10, Qnap 12tb nas, Protectli FW4C-opnsense(=゜ω゜)

zero privacy = zero security . All MX'd Up
UAP = up above people

User avatar
wdscharff
Posts: 1162
Joined: Mon Feb 24, 2020 1:07 am

Re: Image Compression

#8 Post by wdscharff »

Let's drop the subject, since I only collect images on my computers whose copyrights belong to me, that is of course considerably less (around 30,000 and 200GB).

If you had read my text correctly, you would have realised that I have had my share of experience with the compression of larger image collections, starting in the days when people still called mailboxes with analogue modems and worried about every KB. So I don't only see the topic from the point of view of the commercial photographer with a 5000€ camera (whereas as a professional you don't have one but rather several cameras that together cost 5000, so much for redundancy and economy) ;-}

Apart from that, I prefer programmes with a GUI that are suitable for batch processing, there are enough (with which you can also tune the colour subsampling of JPGs, for example, if you know when that makes sense), or you can set not only the dimensions and the degree of sharpening but also the target size in kb/MB.

But we'll leave that, you find the script helpful, I don't. We are not in the same boat now.
my working horse Desktop AMD Ryzen 9 3900x, 32GB Ram // SSD ... enough
mx-fluxbox, what else?

In nature there are neither rewards nor punishments.
There are consequences.


my wallpaper gallery

User avatar
Adrian
Developer
Posts: 9272
Joined: Wed Jul 12, 2006 1:42 am

Re: Image Compression

#9 Post by Adrian »

you find the script helpful, I don't.
To each their own, I agree.

What worries me that this is mislabeled, there's a huge difference between compression (even lossy) and resizing.

User avatar
markol
Posts: 419
Joined: Sat Jan 30, 2021 8:33 am

Re: Image Compression

#10 Post by markol »

I use Trimage image compressor for the web related image optimizing.While it works great for those images, I still wouldn't use it on my photo collection :)

Post Reply

Return to “Tips & Tricks by users (not for help)”