Skip to content

Commit 61a97f3

Browse files
committed
using tk.photoimage instead of PIL
1 parent 161abc8 commit 61a97f3

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

component_sketch.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from tkinter import font
1010
import math
1111
from typing import Any, Callable
12-
from PIL import Image, ImageTk
1312
import os
1413

1514

@@ -2714,30 +2713,50 @@ def draw_battery(self, x_distance, y_distance, scale=1, width=-1, direction='HOR
27142713
return x_distance, y_distance
27152714

27162715
try:
2717-
battery_image = Image.open(image_path)
2716+
battery_photo = tk.PhotoImage(file=image_path)
2717+
2718+
original_width = battery_photo.width()
2719+
original_height = battery_photo.height()
27182720

2719-
original_width, original_height = battery_image.size
27202721
new_width = int(original_width * scale * 0.7)
27212722
new_height = int(original_height * scale * 0.7)
2722-
battery_image = battery_image.resize((new_width, new_height), Image.LANCZOS)
27232723

2724-
battery_photo = ImageTk.PhotoImage(battery_image)
2724+
scale_x = new_width / original_width
2725+
scale_y = new_height / original_height
2726+
2727+
if scale_x >= 1:
2728+
zoom_x = int(scale_x)
2729+
battery_photo = battery_photo.zoom(zoom_x, 1)
2730+
else:
2731+
subsample_x = int(1 / scale_x)
2732+
battery_photo = battery_photo.subsample(subsample_x, 1)
2733+
2734+
if scale_y >= 1:
2735+
zoom_y = int(scale_y)
2736+
battery_photo = battery_photo.zoom(1, zoom_y)
2737+
else:
2738+
subsample_y = int(1 / scale_y)
2739+
battery_photo = battery_photo.subsample(1, subsample_y)
2740+
2741+
new_width = battery_photo.width()
2742+
new_height = battery_photo.height()
27252743

27262744
except Exception as e:
27272745
print(f"Error loading battery image: {e}")
27282746
return x_distance, y_distance
27292747

27302748
battery_obj = self.canvas.create_image(
2731-
x_distance - 10,
2732-
y_distance,
2749+
x_distance - 10,
2750+
y_distance,
27332751
anchor='nw',
27342752
image=battery_photo,
2735-
tags=()
2753+
tags=(battery_id,)
27362754
)
27372755

27382756
if not hasattr(self, 'image_references'):
27392757
self.image_references = []
27402758
self.image_references.append(battery_photo)
2759+
27412760
neg_wire_offset_x = 0 # Left edge
27422761
neg_wire_offset_y = new_height * 0.2 # 20% from the top
27432762

0 commit comments

Comments
 (0)