Image Resize#
Background#
I wanted to create my own sticker pack in Telegram. But the Telegram Bot have a dimenssion requirements where it needs either side of the images to be 512px.
The problem is all images are in all different dimensions and it is a trouble to manually change the dimensions to fit the 512px requirements.
Hence I codeed a python script to do all the adjustments automatically
Link to repo : Image Resize
The Code#
from PIL import Image
import os
os.chdir("image") # create a image file
for each in os.listdir():
if each.endswith("jpg") or each.endswith("JPG") or each.endswith("png") or each.endswith("PNG"):
photo = Image.open(each)
length=photo.size[0]
width=photo.size[1]
if length > width:
ratio = length/512
new_length=512
new_width=round(width/ratio)
else:
ratio = width/512
new_width=512
new_length=round(length/ratio)
photo= photo.resize((new_length,new_width), Image.ANTIALIAS)
photo.save('mini' + each, quality =95)
Explanation#
- Mainly it is the PIL packages
- The script will loop through all the files in a dir called
imageand find any files that isJPGorPNG - Then it will get the length and width of the image
- Find the longer end will be down size to 512 and the shorter end will be down to the respective length ratio
and the whole image will be saved with a new name
minitogether with the old name