Image Compression

Origin

I have so many files on my computer. Like so many files. I noticed that a majority of files were pictures or videos. Most videos I don't want to compress because that would require rewriting the whole file and a lot of time. I was like let me just do photos and see how that goes.

Overview

My objective is to create a program that can compress the image, and make the hard decision of lossless and lossy.

The Difference

When explaining this to someone the first question they ask is, why would you ever want Lossy compression? To this question as simple as possible, do you always use everything? I will use an example from psychology, do you remember what cars were around you while driving this morning. Most likely not so unlike a computer you omitted information that was not needed. In this case that would be the metadata. I wrote a program to preserve the metadata from one file to another file, but the only use is a backup. Like trying to find the location of a certain photo or the specs of the camera/phone.

Lossy

The difference in the Lossy Program and Lossless is the preservation of metadata and because of the writing variation, <1% pixel differential. So now moving onto the program

Step 1 - Handling metadata

This is the only step that separates my program from other ones. Whenever reading the file, I stored the program data into a different file so it can be seen later. I am going to leave that code out of this because in my opinion, it isn't that big of a deal.

Exporting Metadata

As I would try and export it from one photo to another that never worked so I came up with a solution. Exporting the metadata to a JSON file.

Step 2 - User Interface

Giving options was the best idea because sometimes I wanted to do everything and other times just 1 folder so that is where I get the men options Idea.

First up are the helper functions that are called through the program multiple times which made it easier to read. These are simple interface responses and redundant tasks. In comments describe what each one does.

Then we have each function. I could split them up, but feel the comments make it valid for the explanations

Step 3 - Verification

Of course, at first, I would try and create my own version of it that was brute force which works... But is incredibly slow. Originally this code was for finding duplicate images...

I found someone else's code to put a comparison on what the similarity of the images was. Of course, this was nothing compared to the human eye which was the deciding factor but knowing the compression rate was very useful.

Overall

It was a good program and overall has proven to be vitally useful in many situations. 50% compression is insane when you think about it when dealing with compression. Like zipping up an image and you get maybe 90%. In the future, I would hopefully do PNG compression as well.

Drawbacks

It was not smooth sailing the whole time. I wanted to get the metadata transferred over but that just never worked. The program can only do JPEG/JPG files which are very limited. Trying to transfer XMP data was a complete fail and wasted at least 2 hours

Future ideas

Try different file formats

Print cleanly the results and not just number validity

Alternative

I found someone else with a compression that does both jpg and png files and thought it was worth mentioning.

Last updated

Was this helpful?