From e9ad8c23b33c47bea6774a0d4fd02ca164ef9865 Mon Sep 17 00:00:00 2001 From: norman-andrians Date: Sat, 23 Sep 2023 03:38:15 +0700 Subject: [PATCH] resize image based width to keep the text size --- main.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 1070866..5b8622f 100644 --- a/main.py +++ b/main.py @@ -12,14 +12,25 @@ from email.mime.image import MIMEImage app = Flask(__name__) -INPUT_IMG_PATH = "images/main/shannon.jpg" -OUTPUT_IMG_NAME = "black-man-wearing-suit.jpg" +INPUT_IMG_PATH = "images/main/shannon.jpg" # image path +OUTPUT_IMG_NAME = "black-man-wearing-suit.jpg" # name of the file to be added in the email + +# this is actually useless cuz Pillow has provided a thumbnail function to keep the aspect ratio +def resize(in_width, in_height): + out_width = 270 + out_height = in_height * (out_width / in_width) + out_size = (int(out_width), int(out_height)) + + return out_size def add_textimg(text, code): path = f"images/output/pos-{str(code)}.jpg" try: img = Image.open(INPUT_IMG_PATH) + new_size = resize(img.width, img.height) + img = img.resize(new_size) + draw = ImageDraw.Draw(img) font = ImageFont.truetype("fonts/impact.ttf", 24)