Square up images

This commit is contained in:
Cole Deck 2024-05-08 13:04:43 -05:00
parent 1f69dbdb82
commit f1f2e3191b

View File

@ -16,6 +16,7 @@ from util import win32
import os
import glob
import sys
from PIL import Image
def touch(path):
with open(path, 'a'):
@ -67,6 +68,23 @@ def find_file_noext(directory, prefix="part-hires"):
print(directory, matching_files)
return matching_files
def rotate_and_crop_image(path, image_name):
# Open the image file
image_path = path + "/" + image_name
with Image.open(image_path) as img:
# Check if the image is wider than it is tall
if img.width > img.height * 1.2:
# Rotate the image by 90 degrees counter-clockwise
img = img.rotate(90, expand=True)
# Determine the size of the square (the length of the shorter side of the image)
square_size = min(img.width, img.height)
# Crop the image to a square from the top
img_cropped = img.crop((0, 0, square_size, square_size))
# Save or display the image
img_cropped.save(path + "/" + "thumbnail-" + image_name) # Save the cropped image
def parse(filename, output_dir, partnum, dstype, weburl):
tables = []
@ -166,9 +184,11 @@ def parse(filename, output_dir, partnum, dstype, weburl):
count += 1
if os.path.exists(output_dir + "/found_part_hires"):
img = weburl + find_file_noext(output_dir, prefix="part-hires")[0]
rotate_and_crop_image(output_dir, find_file_noext(output_dir, prefix="part-hires")[0])
img = weburl + find_file_noext(output_dir, prefix="thumbnail-part-hires")[0]
elif len(find_file_noext(output_dir, prefix="part")) > 0:
img = weburl + find_file_noext(output_dir, prefix="part")[0]
rotate_and_crop_image(output_dir, find_file_noext(output_dir, prefix="part")[0])
img = weburl + find_file_noext(output_dir, prefix="thumbnail-part")[0]
else:
img = None