From 61a97f381c4405f3bd4dfc62c1ef6790ea182779 Mon Sep 17 00:00:00 2001 From: abbou169 Date: Tue, 19 Nov 2024 21:37:51 -0500 Subject: [PATCH] using tk.photoimage instead of PIL --- component_sketch.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/component_sketch.py b/component_sketch.py index e85dffc..f34eba7 100644 --- a/component_sketch.py +++ b/component_sketch.py @@ -9,7 +9,6 @@ from tkinter import font import math from typing import Any, Callable -from PIL import Image, ImageTk import os @@ -2714,30 +2713,50 @@ def draw_battery(self, x_distance, y_distance, scale=1, width=-1, direction='HOR return x_distance, y_distance try: - battery_image = Image.open(image_path) + battery_photo = tk.PhotoImage(file=image_path) + + original_width = battery_photo.width() + original_height = battery_photo.height() - original_width, original_height = battery_image.size new_width = int(original_width * scale * 0.7) new_height = int(original_height * scale * 0.7) - battery_image = battery_image.resize((new_width, new_height), Image.LANCZOS) - battery_photo = ImageTk.PhotoImage(battery_image) + scale_x = new_width / original_width + scale_y = new_height / original_height + + if scale_x >= 1: + zoom_x = int(scale_x) + battery_photo = battery_photo.zoom(zoom_x, 1) + else: + subsample_x = int(1 / scale_x) + battery_photo = battery_photo.subsample(subsample_x, 1) + + if scale_y >= 1: + zoom_y = int(scale_y) + battery_photo = battery_photo.zoom(1, zoom_y) + else: + subsample_y = int(1 / scale_y) + battery_photo = battery_photo.subsample(1, subsample_y) + + new_width = battery_photo.width() + new_height = battery_photo.height() except Exception as e: print(f"Error loading battery image: {e}") return x_distance, y_distance battery_obj = self.canvas.create_image( - x_distance - 10, - y_distance, + x_distance - 10, + y_distance, anchor='nw', image=battery_photo, - tags=() + tags=(battery_id,) ) if not hasattr(self, 'image_references'): self.image_references = [] self.image_references.append(battery_photo) + neg_wire_offset_x = 0 # Left edge neg_wire_offset_y = new_height * 0.2 # 20% from the top