Created
September 25, 2021 01:49
-
-
Save tudoanh/9d05f3778a819d86e5127a2a0594015e to your computer and use it in GitHub Desktop.
Convert images to required format of Telegram sticker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from pathlib import Path | |
import glob | |
f_names = glob.glob("./Meme/*.jpg") ## Change this to your directory that hold the memes | |
for f in f_names: | |
name = Path(f).stem | |
img = Image.open(f) | |
if img.width > 512 and img.height <= 512: | |
img = img.resize((512, img.height)) | |
print(1) | |
if img.width <= 512 and img.height > 512: | |
img = img.resize((img.width, 512)) | |
print(2) | |
if img.width < img.height < 512: | |
img = img.resize((int(round(img.width / img.height * 512)), 512)) | |
print(3) | |
if img.height < img.width < 512: | |
img = img.resize((512, int(round(img.height / img.width * 512)))) | |
print(4) | |
if img.width == img.height and img.width < 512: | |
img = img.resize((512, 512)) | |
print(5) | |
if img.width > 512 and img.height > 512: | |
img.thumbnail((512, 512)) | |
print(6) | |
img.save(f"./converted/{name}.png") ## `mkdir converted` first |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment