Imdix - An image resizing tool

Some time ago I was filling out a registration form(for something I have got no clue right now) where I had to upload an image of myself. So naturally, I uploaded my image, and suddenly there was a red line saying you have exceeded image file size. Unfortunately, there was a limit of 100KB for the image file(and my image was of 2MB). So I had to lookup online image resizing sites and try with random dimensions to get it to work.

Then I struck me, why can't I automate this?

Why spend 5 minutes doing something when you can try automating it for 2 hours -Unknown

So I started to think. And from the Image processing classes I took, the formula for calculating the size of an image is H x W x C x n_bits.

  • H: Height of the image
  • W: Width of the image
  • C: #Channels
  • n_bits: #bits used to represent the image

So basically, (H x W x C x n_bits)/(8 * 1024) should give you the size of the image in KBs. I went ahead and tested this. And somehow I'm getting different sizes(the equation is wrong?). So I googled, and found out that I haven't accounted for the compression ratio in different image formats. There was no way to get that information from an image(Correct me if I'm wrong).

"Back to square one"

Then my thought process was,

I have an image of size, let's say, 2000KB, and I want to reduce its size to 200KB. The minimum size of an image is 0KB(that's when all four variables are ZERO). So I have numbers(sizes) from 0 to 2000 in ascending order(obviously) and I need to search for a number(size) "200" in between. Which search algorithm should I use?

"Binary Search"

Now I could manipulate the image dimensions(H and W) by using image file size as a variable for binary search, Kudos.

Install

pip install --upgrade imdix

Usage

usage: dx [-h] --image IMAGE --size SIZE [--output OUTPUT] [--ext EXT]

Dynamic image file resizer

optional arguments:
  -h, --help            show this help message and exit
  --image IMAGE, -i IMAGE
                        Path to image file
  --size SIZE, -s SIZE  Target output size
  --output OUTPUT, -o OUTPUT
                        Output file name
  --ext EXT, -x EXT     Extension of output file

I don't know if there exist any other tools to do this task. Anyway, I decided to make one of my own. This project needs a lot of improvements and features. Feedback and contributions are welcome.

Link to project: https://github.com/amalshaji/imdix

No Comments Yet