Skip to content

Commit 833b83d

Browse files
committed
fix too large images failing
1 parent e3b06b1 commit 833b83d

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

battle_map_tv/image.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
class CustomGraphicsPixmapItem(QGraphicsPixmapItem):
2020
def __init__(self, image_path: str):
2121
pixmap = QPixmap(image_path)
22+
if pixmap.isNull():
23+
raise ValueError(f"Failed to load '{image_path}'")
2224
super().__init__(pixmap)
2325
self.image_filename = os.path.basename(image_path)
2426
self.setFlag(self.GraphicsItemFlag.ItemIsMovable)
@@ -90,12 +92,7 @@ def __init__(
9092
ImageKeys.scale,
9193
)
9294
except KeyError:
93-
scale_fit_image = min(
94-
window_width_px / self.pixmap_item.pixmap().width(),
95-
window_height_px / self.pixmap_item.pixmap().height(),
96-
)
97-
if scale_fit_image < 1.0:
98-
self.scale(scale_fit_image)
95+
self._fit_image_to_window(window_width_px, window_height_px)
9996
else:
10097
self.scale(scale)
10198

@@ -109,6 +106,12 @@ def __init__(
109106
else:
110107
self.pixmap_item.set_position(position)
111108

109+
def _fit_image_to_window(self, window_width_px: int, window_height_px: int):
110+
image_width = self.pixmap_item.pixmap().width()
111+
image_height = self.pixmap_item.pixmap().height()
112+
scale = min(window_width_px / image_width, window_height_px / image_height)
113+
self.scale(scale)
114+
112115
def delete(self):
113116
self.scene.removeItem(self.pixmap_item)
114117

battle_map_tv/window_image.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Optional, Callable
22

33
from PySide6.QtCore import Qt
4-
from PySide6.QtGui import QMouseEvent
4+
from PySide6.QtGui import QMouseEvent, QImageReader
55
from PySide6.QtWidgets import QGraphicsView, QGraphicsScene
66

77
from battle_map_tv.aoe import AreaOfEffectManager
@@ -26,6 +26,8 @@ def __init__(self):
2626
self.setAlignment(Qt.AlignCenter) # type: ignore[attr-defined]
2727
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) # type: ignore[attr-defined]
2828
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) # type: ignore[attr-defined]
29+
# allow for large size images
30+
QImageReader.setAllocationLimit(0)
2931

3032
scene = QGraphicsScene()
3133
scene.setSceneRect(0, 0, self.size().width(), self.size().height())

0 commit comments

Comments
 (0)