transferring functionalities from main.py to src
This commit is contained in:
221
src/GUI.py
221
src/GUI.py
@@ -1,75 +1,146 @@
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog
|
||||
from PIL import ImageTk
|
||||
from ImageContainer import ImageContainer
|
||||
from ImageManipulation.ManipulationList import *
|
||||
|
||||
|
||||
class GUI:
|
||||
"""The GUI class responsible for the main application GUI.
|
||||
|
||||
@warning This class is a singleton.
|
||||
"""
|
||||
_instance = None
|
||||
|
||||
# If the GUI has been initialised.
|
||||
_isInitialised = False
|
||||
|
||||
_currentImage = None
|
||||
|
||||
def __new__(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = super(GUI, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def initialise(self):
|
||||
"""Initialise the GUI."""
|
||||
|
||||
if self._isInitialised:
|
||||
return
|
||||
else:
|
||||
self._isInitialised = True
|
||||
|
||||
# Main window
|
||||
root = tk.Tk()
|
||||
root.title("Image Viewer")
|
||||
root.geometry("800x600")
|
||||
root.config(bg="white")
|
||||
icon = tk.PhotoImage(file='icon.png')
|
||||
root.tk.call('wm', 'iconphoto', root._w, icon)
|
||||
root.minsize(800, 600)
|
||||
|
||||
# Menus
|
||||
menu = tk.Menu(root)
|
||||
root.config(menu=menu)
|
||||
file_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="File", menu=file_menu)
|
||||
file_menu.add_command(label="Open Image", command=lambda: self._openImage())
|
||||
file_menu.add_command(label="Save Image", command=lambda: _save_image(OPENED_IMAGE))
|
||||
file_menu.add_command(label="Exit", command=root.quit)
|
||||
|
||||
|
||||
test_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Filters", menu=test_menu)
|
||||
for manipulation in GetImageManipulationList():
|
||||
test_menu.add_command(label=manipulation.getManipulationName(), command=lambda: manipulation.manipulateImage())
|
||||
|
||||
# Frame to hold image
|
||||
imgframe = tk.Frame(root, width=500, height=500, bg="lightgray", relief="sunken", bd=2)
|
||||
imgframe.pack(side="top", pady=10)
|
||||
|
||||
# Label to display image
|
||||
image_label = tk.Label(imgframe, width=500, height=500, bg="white")
|
||||
image_label.pack(expand=True)
|
||||
|
||||
root.mainloop()
|
||||
|
||||
def _openImage(self) -> None:
|
||||
file_path = filedialog.askopenfilename(
|
||||
filetypes=[("Image files", "*.jpg *.jpeg *.png *.gif *.bmp")]
|
||||
)
|
||||
if not file_path:
|
||||
return
|
||||
|
||||
self._currentImage = ImageContainer()
|
||||
self._currentImage.loadImage(file_path)
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog
|
||||
from PIL import ImageTk
|
||||
from ImageContainer import ImageContainer
|
||||
from ImageManipulation.ManipulationList import *
|
||||
from functools import partial
|
||||
|
||||
|
||||
class GUI:
|
||||
"""The GUI class responsible for the main application GUI.
|
||||
|
||||
@warning This class is a singleton.
|
||||
"""
|
||||
_instance = None
|
||||
|
||||
# If the GUI has been initialised.
|
||||
_isInitialised = False
|
||||
|
||||
_currentImage = None
|
||||
|
||||
def __new__(cls):
|
||||
if cls._instance is None:
|
||||
cls._instance = super(GUI, cls).__new__(cls)
|
||||
return cls._instance
|
||||
|
||||
def initialise(self):
|
||||
"""Initialise the GUI."""
|
||||
|
||||
if self._isInitialised:
|
||||
return
|
||||
else:
|
||||
self._isInitialised = True
|
||||
|
||||
# Main window
|
||||
root = tk.Tk()
|
||||
root.title("Image Viewer")
|
||||
root.geometry("800x600")
|
||||
root.config(bg="white")
|
||||
icon = tk.PhotoImage(file='icon.png')
|
||||
root.tk.call('wm', 'iconphoto', root._w, icon)
|
||||
root.minsize(800, 600)
|
||||
|
||||
# Menus
|
||||
menu = tk.Menu(root)
|
||||
root.config(menu=menu)
|
||||
file_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="File", menu=file_menu)
|
||||
file_menu.add_command(label="Open Image", command=lambda: self._openImage())
|
||||
file_menu.add_command(label="Save Image", command=lambda: self._saveImage())
|
||||
file_menu.add_command(label="Exit", command=root.quit)
|
||||
|
||||
edit_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Edit", menu=edit_menu)
|
||||
edit_menu.add_command(label="Undo", accelerator="Ctrl+Z", command=lambda: self._undo())
|
||||
|
||||
|
||||
test_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Filters", menu=test_menu)
|
||||
for manipulation in GetImageManipulationList():
|
||||
test_menu.add_command(
|
||||
label=manipulation.getManipulationName(),
|
||||
command=partial(self._applyManipulation, manipulation)
|
||||
)
|
||||
|
||||
# Manual Test menu listing all manipulations explicitly
|
||||
manual_menu = tk.Menu(menu, tearoff=0)
|
||||
menu.add_cascade(label="Test", menu=manual_menu)
|
||||
manual_menu.add_command(label="Padding", command=partial(self._applyManipulation, Padding(), {"border_width": 50}))
|
||||
manual_menu.add_command(label="Crop", command=partial(self._applyManipulation, CropImage()))
|
||||
manual_menu.add_command(label="Resize", command=partial(self._applyManipulation, ResizeImage(), {"width": 200, "height": 200}))
|
||||
manual_menu.add_command(label="Copy", command=partial(self._applyManipulation, CopyImage()))
|
||||
manual_menu.add_command(label="Greyscale", command=partial(self._applyManipulation, Grayscale()))
|
||||
manual_menu.add_command(label="HSV", command=partial(self._applyManipulation, HSV()))
|
||||
manual_menu.add_command(label="Hue Shifted", command=partial(self._applyManipulation, HueShift(), {"hue": 50}))
|
||||
manual_menu.add_command(label="Smoothed", command=partial(self._applyManipulation, BoxBlur(), {"ksize": 15}))
|
||||
manual_menu.add_command(label="Rotated", command=partial(self._applyManipulation, RotateImage(), {"angle": 90}))
|
||||
manual_menu.add_command(label="Flip (Horizontal)", command=partial(self._applyManipulation, FlipImage(), {"mode": "horizontal"}))
|
||||
manual_menu.add_command(label="Flip (Vertical)", command=partial(self._applyManipulation, FlipImage(), {"mode": "vertical"}))
|
||||
manual_menu.add_command(label="Color Adjust", command=partial(self._applyManipulation, ColorAdjust(), {"brightness": 10, "contrast": 1.2, "saturation": 1.1}))
|
||||
manual_menu.add_command(label="Gaussian Blur", command=partial(self._applyManipulation, GaussianBlur(), {"ksize": 5}))
|
||||
manual_menu.add_command(label="Sobel Edge", command=partial(self._applyManipulation, SobelEdge(), {"dx": 1, "dy": 0, "ksize": 3}))
|
||||
manual_menu.add_command(label="Binary Threshold", command=partial(self._applyManipulation, BinaryThreshold(), {"thresh": 127}))
|
||||
manual_menu.add_command(label="Histogram Threshold", command=partial(self._applyManipulation, HistogramThreshold()))
|
||||
|
||||
# Frame to hold image
|
||||
imgframe = tk.Frame(root, width=500, height=500, bg="lightgray", relief="sunken", bd=2)
|
||||
imgframe.pack(side="top", pady=10)
|
||||
|
||||
# Label to display image
|
||||
self._imageLabel = tk.Label(imgframe, width=500, height=500, bg="white")
|
||||
self._imageLabel.pack(expand=True)
|
||||
|
||||
self._root = root
|
||||
|
||||
# Key bindings
|
||||
root.bind_all('<Control-z>', lambda event: self._undo())
|
||||
|
||||
root.mainloop()
|
||||
|
||||
def _openImage(self) -> None:
|
||||
file_path = filedialog.askopenfilename(
|
||||
filetypes=[("Image files", "*.jpg *.jpeg *.png *.gif *.bmp")]
|
||||
)
|
||||
if not file_path:
|
||||
return
|
||||
|
||||
self._currentImage = ImageContainer()
|
||||
self._currentImage.loadImage(file_path)
|
||||
self._renderCurrentImage()
|
||||
|
||||
def _renderCurrentImage(self) -> None:
|
||||
if self._currentImage is None or self._currentImage.getImage() is None:
|
||||
return
|
||||
pil_img = self._currentImage.getImage()
|
||||
width, height = pil_img.size
|
||||
tk_img = ImageTk.PhotoImage(pil_img)
|
||||
# Keep reference to avoid garbage collection
|
||||
self._imageLabel.image = tk_img
|
||||
self._imageLabel.config(image=tk_img, width=width, height=height)
|
||||
|
||||
def _applyManipulation(self, manipulation, params: dict | None = None) -> None:
|
||||
if self._currentImage is None:
|
||||
return
|
||||
# Take undo snapshot
|
||||
self._currentImage.snapshot()
|
||||
if params is None:
|
||||
# default demo params for crop
|
||||
params = {"width": 200, "height": 200}
|
||||
manipulation.manipulateImage(self._currentImage, params)
|
||||
self._renderCurrentImage()
|
||||
|
||||
def _saveImage(self) -> None:
|
||||
if self._currentImage is None or self._currentImage.getImage() is None:
|
||||
return
|
||||
path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[
|
||||
("PNG", "*.png"), ("JPEG", "*.jpg;*.jpeg"), ("Bitmap", "*.bmp"), ("All Files", "*.*")
|
||||
])
|
||||
if not path:
|
||||
return
|
||||
self._currentImage.saveImage(path)
|
||||
|
||||
def _undo(self) -> None:
|
||||
if self._currentImage is None:
|
||||
return
|
||||
self._currentImage.undo()
|
||||
self._renderCurrentImage()
|
||||
Reference in New Issue
Block a user