adding copy image from canvas; adding paste image to canvas

This commit is contained in:
vb
2025-11-07 14:51:01 +01:00
parent eaa6b6f4d7
commit e6599a1cd7
6 changed files with 384 additions and 1 deletions

View File

@@ -12,6 +12,8 @@ from utils.text_entry import TextEntryHandler
from utils.file_operations import FileOperationsHandler
from utils.image_properties import ImagePropertiesHandler
from utils.filter_params import FilterParamsHandler
from ImageManipulation.CopyToClipboard import CopyToClipboard
from ImageManipulation.PasteFromClipboard import PasteFromClipboard
from functools import partial
@@ -126,6 +128,8 @@ class GUI:
filter_menu.add_command(label="Histogram Thresholding", command=lambda: self._applyHistogramThreshold())
clipboard_menu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="Clipboard", menu=clipboard_menu)
clipboard_menu.add_command(label="Copy Image", accelerator="Ctrl+C", command=lambda: self._copyImage())
clipboard_menu.add_command(label="Paste Image", accelerator="Ctrl+V", command=lambda: self._pasteImage())
tools_menu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="Tools", menu=tools_menu)
tools_menu.add_command(label="Zoom In", accelerator="Ctrl+Plus", command=lambda: self._zoomIn())
@@ -239,6 +243,8 @@ class GUI:
root.bind_all('<Control-plus>', lambda event: self._zoomIn())
root.bind_all('<Control-equal>', lambda event: self._zoomIn()) # Plus key without shift
root.bind_all('<Control-minus>', lambda event: self._zoomOut())
root.bind_all('<Control-c>', lambda event: self._copyImage())
root.bind_all('<Control-v>', lambda event: self._pasteImage())
root.mainloop()
@@ -567,6 +573,59 @@ class GUI:
params = {"scale": 0.8}
self._applyManipulation(manipulation, params)
def _copyImage(self) -> None:
"""Copy the current image to the system clipboard."""
if self._currentImage is None or self._currentImage.getImage() is None:
messagebox.showwarning("No Image", "Please load an image first.")
return
manipulation = CopyToClipboard()
manipulation.manipulateImage(self._currentImage, {})
# Check if copy was successful
if self._currentImage._clipboard_copy_success:
messagebox.showinfo("Copy", "Image copied to clipboard.")
else:
messagebox.showerror(
"Copy Error",
"Failed to copy image to clipboard.\n\n"
"Windows: Please install pywin32 (pip install pywin32)\n"
"Linux: Please install xclip (sudo apt-get install xclip)"
)
# Clean up the attribute
delattr(self._currentImage, '_clipboard_copy_success')
def _pasteImage(self) -> None:
"""Paste an image from the system clipboard."""
manipulation = PasteFromClipboard()
# If no current image exists, create a new ImageContainer
if self._currentImage is None or self._currentImage.getImage() is None:
self._currentImage = ImageContainer()
manipulation.manipulateImage(self._currentImage, {})
# Check if paste was successful
if hasattr(self._currentImage, '_clipboard_paste_success'):
if self._currentImage._clipboard_paste_success:
# Re-render the image
self._renderCurrentImage()
messagebox.showinfo("Paste", "Image pasted from clipboard.")
else:
error_msg = getattr(self._currentImage, '_clipboard_paste_error',
"Failed to paste image from clipboard.")
messagebox.showerror(
"Paste Error",
f"{error_msg}\n\n"
"Windows: Please install pywin32 (pip install pywin32)\n"
"Linux: Please install xclip (sudo apt-get install xclip)"
)
# Clean up the attributes
if hasattr(self._currentImage, '_clipboard_paste_success'):
delattr(self._currentImage, '_clipboard_paste_success')
if hasattr(self._currentImage, '_clipboard_paste_error'):
delattr(self._currentImage, '_clipboard_paste_error')
def _openCamera(self):
camera = CameraWindow()
camera.run(self)