Use imagetools.fit_into_bounds.

This commit is contained in:
voussoir 2021-08-09 08:31:27 -07:00
parent 74bd773f33
commit 4b2209d7ef
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -89,6 +89,8 @@ import os
import PIL.Image import PIL.Image
import sys import sys
from voussoirkit import imagetools
ICO_HEADER_LENGTH = 6 ICO_HEADER_LENGTH = 6
ICON_DIRECTORY_ENTRY_LENGTH = 16 ICON_DIRECTORY_ENTRY_LENGTH = 16
BMP_HEADER_LENGTH = 40 BMP_HEADER_LENGTH = 40
@ -116,27 +118,14 @@ def chunk_sequence(sequence, chunk_length, allow_incomplete=True):
return chunks return chunks
def fit_into_bounds(iw, ih, fw, fh):
'''
Given the w+h of the image and the w+h of the frame,
return new w+h that fits the image into the frame
while maintaining the aspect ratio and leaving blank space
everywhere else
'''
ratio = min(fw/iw, fh/ih)
w = int(iw * ratio)
h = int(ih * ratio)
return (w, h)
def little(x, length): def little(x, length):
return x.to_bytes(length, byteorder='little') return x.to_bytes(length, byteorder='little')
def load_image(filename): def load_image(filename):
image = PIL.Image.open(filename) image = PIL.Image.open(filename)
if min(image.size) > 256: (w, h) = image.size
(w, h) = image.size (new_w, new_h) = imagetools.fit_into_bounds(w, h, 256, 256, only_shrink=True)
image = image.resize(fit_into_bounds(w, h, 256, 256), resample=PIL.Image.ANTIALIAS) image = image.resize((new_w, new_h), resample=PIL.Image.ANTIALIAS)
image = image.convert('RGBA') image = image.convert('RGBA')
return image return image