Resize images with my blog naming convention

This commit is contained in:
John McCardle 2022-01-22 10:42:23 -05:00
parent 9fc944e1dd
commit 65316276dc
1 changed files with 17 additions and 3 deletions

View File

@ -2,15 +2,29 @@
import os import os
import json import json
from datetime import datetime, date from datetime import datetime, date
import cv2
def markdown_date(ts): def markdown_date(ts):
d = datetime.fromtimestamp(ts) d = datetime.fromtimestamp(ts)
return f"{d.year}-{d.month}-{d.day}" return f"{d.year}-{d.month}-{d.day}"
IMG_WIDTH = 760 IMG_WIDTH = 760
def resize_rename(imgpath, article_number, image_number): def resize_rename(imgpath, article_number, image_number, destpath=None, target_width=IMG_WIDTH):
new_fn = f"article{article_number.zfill(2)}_image{image_number.zfill(2)}.{imgpath.split('.')[-1]}" 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: class BlogScaffold:
def __init__(self, path): def __init__(self, path):