transferring functionalities from main.py to src
This commit is contained in:
@@ -1,25 +1,51 @@
|
||||
import cv2
|
||||
from PIL import Image, ImageTk
|
||||
|
||||
class ImageContainer:
|
||||
_imageData = None
|
||||
_path = None
|
||||
|
||||
def loadImage(self, path: str) -> None:
|
||||
""" Load image file from the path.
|
||||
|
||||
:param path: Path to the image file.
|
||||
"""
|
||||
imgcv2 = cv2.imread(path)
|
||||
height, width, channels = imgcv2.shape
|
||||
|
||||
# Open and resize image (optional)
|
||||
_imageData = Image.open(path)
|
||||
_imageData = _imageData.resize((height, width), Image.LANCZOS) # Resize to fit frame
|
||||
|
||||
_path = path
|
||||
|
||||
print("Opened image:", path)
|
||||
|
||||
def getDimensions(self) -> tuple[int,int]:
|
||||
return self._imageData.shape
|
||||
import cv2
|
||||
from PIL import Image, ImageTk
|
||||
|
||||
class ImageContainer:
|
||||
_imageData = None
|
||||
_path = None
|
||||
_history = None
|
||||
|
||||
def loadImage(self, path: str) -> None:
|
||||
""" Load image file from the path.
|
||||
|
||||
:param path: Path to the image file.
|
||||
"""
|
||||
imgcv2 = cv2.imread(path)
|
||||
height, width, channels = imgcv2.shape
|
||||
|
||||
# Open and resize image (optional)
|
||||
self._imageData = Image.open(path)
|
||||
# PIL expects (width, height)
|
||||
self._imageData = self._imageData.resize((width, height), Image.LANCZOS)
|
||||
|
||||
self._path = path
|
||||
self._history = []
|
||||
|
||||
print("Opened image:", path)
|
||||
|
||||
def getDimensions(self) -> tuple[int,int]:
|
||||
# PIL Image.size returns (width, height)
|
||||
return self._imageData.size if self._imageData is not None else (0, 0)
|
||||
|
||||
def getImage(self) -> Image:
|
||||
return self._imageData
|
||||
|
||||
def saveImage(self, path: str) -> None:
|
||||
if self._imageData is not None:
|
||||
self._imageData.save(path)
|
||||
|
||||
def snapshot(self) -> None:
|
||||
"""Push a copy of current image to history for undo."""
|
||||
if self._imageData is None:
|
||||
return
|
||||
# Ensure a deep copy (PIL copy is sufficient)
|
||||
self._history.append(self._imageData.copy())
|
||||
# Cap history size to avoid memory blow-up
|
||||
if len(self._history) > 20:
|
||||
self._history.pop(0)
|
||||
|
||||
def undo(self) -> None:
|
||||
if not self._history:
|
||||
return
|
||||
self._imageData = self._history.pop()
|
||||
Reference in New Issue
Block a user