Files » ImageIO.py
| 1 |
import cv2 as cv |
|---|---|
| 2 |
import numpy as np |
| 3 |
import os |
| 4 |
from PIL import Image |
| 5 |
import tkinter as tk |
| 6 |
|
| 7 |
class ImageIO: |
| 8 |
def __init__(self, shared_data): |
| 9 |
self.shared_data = shared_data |
| 10 |
|
| 11 |
def load_image(self, path): |
| 12 |
return cv.imread(path, cv.IMREAD_COLOR) |
| 13 |
|
| 14 |
def write_image(self, img, fileName="image.png"): |
| 15 |
if cv.imwrite(fileName, img): |
| 16 |
print(f"File {fileName} Written Successfully") |
| 17 |
else: |
| 18 |
print("File writing failed") |
| 19 |
|
| 20 |
def show_image(self, img, title="Image Title", canvas=None): |
| 21 |
# Set up OpenCV fullscreen window in the main thread
|
| 22 |
cv.namedWindow(title, cv.WINDOW_NORMAL) |
| 23 |
cv.setWindowProperty(title, cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN) |
| 24 |
|
| 25 |
# Display the image in OpenCV and keep it open without blocking Tkinter
|
| 26 |
cv.imshow(title, img) |
| 27 |
|
| 28 |
# Run a non-blocking loop to keep OpenCV window and Tkinter GUI responsive
|
| 29 |
while cv.getWindowProperty(title, cv.WND_PROP_VISIBLE) >= 1: |
| 30 |
cv.waitKey(1) |
| 31 |
if canvas: |
| 32 |
canvas.update_idletasks() |
| 33 |
canvas.update() |
| 34 |
|
| 35 |
# Close the OpenCV window once the loop ends
|
| 36 |
cv.destroyAllWindows() |
| 37 |
|
| 38 |
|