From 65316276dc6f1f0a80f728f3a69a8f1298641c5e Mon Sep 17 00:00:00 2001 From: John McCardle Date: Sat, 22 Jan 2022 10:42:23 -0500 Subject: [PATCH] Resize images with my blog naming convention --- backblogger.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/backblogger.py b/backblogger.py index b851239..2e80dfd 100644 --- a/backblogger.py +++ b/backblogger.py @@ -2,15 +2,29 @@ import os import json from datetime import datetime, date +import cv2 def markdown_date(ts): d = datetime.fromtimestamp(ts) return f"{d.year}-{d.month}-{d.day}" IMG_WIDTH = 760 -def resize_rename(imgpath, article_number, image_number): - new_fn = f"article{article_number.zfill(2)}_image{image_number.zfill(2)}.{imgpath.split('.')[-1]}" - +def resize_rename(imgpath, article_number, image_number, destpath=None, target_width=IMG_WIDTH): + new_fn = f"article{str(article_number).zfill(2)}_image{str(image_number).zfill(2)}.{imgpath.split('.')[-1]}" + if destpath is None: + destpath = os.path.join('.', imgpath.replace(os.path.basename(imgpath), '')) + dest_fn = os.path.join(destpath, new_fn) + img = cv2.imread(imgpath) + h, w = img.shape[:2] + print(h, w) + if w <= target_width: + cv2.imwrite(dest_fn, img) + return + ratio = target_width / float(w) + new_h = int(h * ratio) + print(ratio, target_width, new_h) + new_img = cv2.resize(img, (target_width, new_h), interpolation=cv2.INTER_AREA) + cv2.imwrite(dest_fn, new_img) class BlogScaffold: def __init__(self, path):