From f1f2e3191b9f34dfcfa744b4a9947717f0d9d5ce Mon Sep 17 00:00:00 2001 From: Cole Deck Date: Wed, 8 May 2024 13:04:43 -0500 Subject: [PATCH] Square up images --- read_datasheet.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/read_datasheet.py b/read_datasheet.py index 6e72d23..ab8ff10 100755 --- a/read_datasheet.py +++ b/read_datasheet.py @@ -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