|
9 | 9 | from tkinter import font |
10 | 10 | import math |
11 | 11 | from typing import Any, Callable |
12 | | -from PIL import Image, ImageTk |
13 | 12 | import os |
14 | 13 |
|
15 | 14 |
|
@@ -2714,30 +2713,50 @@ def draw_battery(self, x_distance, y_distance, scale=1, width=-1, direction='HOR |
2714 | 2713 | return x_distance, y_distance |
2715 | 2714 |
|
2716 | 2715 | 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() |
2718 | 2720 |
|
2719 | | - original_width, original_height = battery_image.size |
2720 | 2721 | new_width = int(original_width * scale * 0.7) |
2721 | 2722 | new_height = int(original_height * scale * 0.7) |
2722 | | - battery_image = battery_image.resize((new_width, new_height), Image.LANCZOS) |
2723 | 2723 |
|
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() |
2725 | 2743 |
|
2726 | 2744 | except Exception as e: |
2727 | 2745 | print(f"Error loading battery image: {e}") |
2728 | 2746 | return x_distance, y_distance |
2729 | 2747 |
|
2730 | 2748 | battery_obj = self.canvas.create_image( |
2731 | | - x_distance - 10, |
2732 | | - y_distance, |
| 2749 | + x_distance - 10, |
| 2750 | + y_distance, |
2733 | 2751 | anchor='nw', |
2734 | 2752 | image=battery_photo, |
2735 | | - tags=() |
| 2753 | + tags=(battery_id,) |
2736 | 2754 | ) |
2737 | 2755 |
|
2738 | 2756 | if not hasattr(self, 'image_references'): |
2739 | 2757 | self.image_references = [] |
2740 | 2758 | self.image_references.append(battery_photo) |
| 2759 | + |
2741 | 2760 | neg_wire_offset_x = 0 # Left edge |
2742 | 2761 | neg_wire_offset_y = new_height * 0.2 # 20% from the top |
2743 | 2762 |
|
|
0 commit comments