Viktor Shchelochkov
5 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with
6 additions and
3 deletions
-
sticker/lib/util.py
|
|
|
@ -28,9 +28,8 @@ open_utf8 = partial(open, encoding='UTF-8') |
|
|
|
|
|
|
|
def convert_image(data: bytes, max_w=256, max_h=256) -> (bytes, int, int): |
|
|
|
image: Image.Image = Image.open(BytesIO(data)).convert("RGBA") |
|
|
|
new_file = BytesIO() |
|
|
|
image.save(new_file, "png") |
|
|
|
w, h = image.size |
|
|
|
|
|
|
|
if w > max_w or h > max_h: |
|
|
|
# Set the width and height to lower values so clients wouldn't show them as huge images |
|
|
|
if w > h: |
|
|
|
@ -39,6 +38,10 @@ def convert_image(data: bytes, max_w=256, max_h=256) -> (bytes, int, int): |
|
|
|
else: |
|
|
|
w = int(w / (h / max_h)) |
|
|
|
h = max_h |
|
|
|
image = image.resize((w, h), Image.LANCZOS) |
|
|
|
|
|
|
|
new_file = BytesIO() |
|
|
|
image.save(new_file, "png") |
|
|
|
return new_file.getvalue(), w, h |
|
|
|
|
|
|
|
|
|
|
|
|