Project

General

Profile

Files » ImageIO.py

Updated fullscreen version - Md Tamjidur RAHMAN, 10/31/2024 02:57 PM

 
1
import cv2 as cv
2
import numpy as np
3
import tkinter as tk
4
from tkinter import filedialog, messagebox
5
import os
6
from PIL import Image, ImageTk
7

    
8
class ImageIO:
9
    def __init__(self, shared_data):
10
        self.shared_data = shared_data
11

    
12
    def load_image(self, path):
13
        return cv.imread(path, cv.IMREAD_COLOR)
14

    
15
    def write_image(self, img, fileName="image.png"):
16
        if cv.imwrite(fileName, img):
17
            print(f"File {fileName} Written Successfully")
18
        else:
19
            print("File writing failed")
20

    
21
    def show_image(self, img, title="Image Title", canvas=None):
22
        img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
23
        img_pil = Image.fromarray(img_rgb)
24

    
25
        # Create a full-screen window
26
        window = tk.Toplevel()
27
        window.attributes("-fullscreen", True)
28
        window.bind("<Escape>", lambda e: window.destroy())  # Bind Esc key to exit full screen
29

    
30
        # Get screen dimensions
31
        screen_width = window.winfo_screenwidth()
32
        screen_height = window.winfo_screenheight()
33

    
34
        # Adjust the image to fit the screen
35
        img_resized = img_pil.resize((screen_width, screen_height), Image.LANCZOS)  # Use LANCZOS for high-quality resizing
36
        img_tk_resized = ImageTk.PhotoImage(img_resized)
37

    
38
        # Create a canvas and pack it to fill the window
39
        canvas = tk.Canvas(window, width=screen_width, height=screen_height)
40
        canvas.pack(fill=tk.BOTH, expand=True)
41

    
42
        # Align image to the center of the canvas
43
        canvas.create_image(screen_width // 2, screen_height // 2, anchor=tk.CENTER, image=img_tk_resized)
44

    
45
        canvas.image = img_tk_resized  # Prevent image from being garbage collected
46
        window.title(title)
47
        window.mainloop()
(29-29/49)