0

I have a bunch of images that aren't equal size, and where some fit entirely to the frame and some have blank padding.

I would like to know how I can resize each of them to be the same image size and to have roughly the same border size.

Currently I am doing

from PIL import Image
from glob import glob

images = glob('src/assets/emotes/medals/**/*.png', recursive=True)

for image_path in images:
    im = Image.open(image_path).convert('RGBA')
    im = im.resize((100, 100))
    im.save(image_path)

but this doesn't account for a possible border.

Image 1 - 101 x 101
image 1 w/ border image 1

Image 2 - 132 x 160
image 2 w/ border image 2

Desired result - 100 x 100
desired 1 w/ border desired 1

desired 2 w/ border desired 2

Images arent always bigger than (100, 100) so I will need to use resize.

I can also maybe remove the PNG border for all images, and then resize which might be easier.

AvidProgrammer
  • 103
  • 2
  • 8
  • 1
    As far as I know, `im.getbbox()` will give u the original image without the transparent background. But this seems to go in similar direction: https://stackoverflow.com/questions/1905421/crop-a-png-image-to-its-minimum-size – Bijay Regmi Oct 26 '21 at 10:59
  • thank you that works, could you post that as answer? – AvidProgrammer Oct 26 '21 at 11:02

1 Answers1

1

Taken from Crop a PNG image to its minimum size, im.getbbox() will give you the original image without transparent background.

Documentation : Pillow (PIL Fork)

Bijay Regmi
  • 1,187
  • 2
  • 11
  • 25