PNG cropping increases file size

63 Views Asked by At

I have a set of PNG 240x240px images (~1Gb of images) for training a classification algorithm and I need to crop the bottom 26px (resulting in 240x214px) from those images, which contain some number and date which I do not want in the final image set.

I've started with a python library named Pillow, but soon realised that after reading, cropping and resaving the PNG images the file size increased about 5x (~5Gb of images).

I think I know it will not affect the outcome of the CNN, but mainly the time it will take to train it, but I still wonder if there is a way to better compress or any other method to keep the original file size.

I will attach one image before (11Kb) and after (74Kb) processing as an example.

enter image description here enter image description here

I'll also attach a link to imgur where the images are in their original PNG format.

This is the code I've used for cropping the images:

from os import listdir
from PIL import Image

def cropp_images_in_folder(from_folder, to_folder):
    for image in listdir(from_folder):
        if image.endswith(".png"):
            full_path = from_folder + image
            im = Image.open(full_path)
            im1 = im.crop((0, 0, 240, 214))
            full_path = to_folder + image
            im1.save(full_path, optimize=True, compress_level = 9)
            i += 1
2

There are 2 best solutions below

2
Mark Adler On

I have a set of PNG 240x240px images ...

The first image is not PNG. It's JPEG. The second image is PNG. If you want a more comparable size for the second image, then compress it to JPEG as well.

1
pmqs On

Just to emphasise the point that Mark has already made, the original file is a JPEG, the cropped file is a PNG.

I downloaded the two file from https://imgur.com/a/LVy3tfp

$ unzip -l ~/Downloads/Imgur\ Album\ Before\ and\ after\ PNG\ crop.zip 
Archive:  /home/paul/Downloads/Imgur Album Before and after PNG crop.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
    74877  2024-03-11 12:44   1 - j5iIxA6.png
    10910  2024-03-11 12:44   2 - 4bGrdyP.jpg
---------                     -------
    85787                     2 files

File extensions suggest a JPEG & a PNG but let's double check by seeing what is actually stored in these file.

$ unzip ~/Downloads/Imgur\ Album\ Before\ and\ after\ PNG\ crop.zip 
Archive:  ~/Downloads/Imgur Album Before and after PNG crop.zip
  inflating: 1 - j5iIxA6.png         
  inflating: 2 - 4bGrdyP.jpg         

$ file *
1 - j5iIxA6.png: PNG image data, 240 x 214, 8-bit/color RGB, non-interlaced
2 - 4bGrdyP.jpg: JPEG image data, baseline, precision 8, 240x240, components 3

Yep, you have a JPEG file & a PNG file.

Did you upload the wrong version of the original file?