Skip to content

Instantly share code, notes, and snippets.

@tudoanh
Created September 25, 2021 01:49
Show Gist options
  • Save tudoanh/9d05f3778a819d86e5127a2a0594015e to your computer and use it in GitHub Desktop.
Save tudoanh/9d05f3778a819d86e5127a2a0594015e to your computer and use it in GitHub Desktop.
Convert images to required format of Telegram sticker
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