Project

General

Profile

Feature #1150 ยป ImageIO.py

Wonil KIM, 10/31/2024 01:54 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
import configparser
8

    
9
class ImageIO:
10
    ## \class This class is in charge of image input and output
11
    def __init__(self, shared_data):
12
        ## \breif
13
        # Initializator for whole method.
14
        # Get parsed config file data from the class 'SharedData'
15
        # \param self the class itself
16
        # \param shared_dara parsed config file
17
        # \return None
18
        #
19
        # \warning This method will not check config data is valid or not
20
        self.shared_data = shared_data
21

    
22
    def load_image(self, path):
23
        ## \breif Function for loading image
24
        # \param path Path of the image
25
        # \return numpy array, image
26
        #
27
        # \warning This method does not check the file format or image content
28
        return cv.imread(path, cv.IMREAD_COLOR)
29

    
30
    def write_image(self, img, fileName="image.png"):
31
        ## \breif Function to manipulate the image
32
        # \param img Loaded image, numpy matrix
33
        # \param fileName Name of the image. "image.png" will be the default vale if there are no input
34
        # \return None
35
        if cv.imwrite(fileName, img):
36
            print(f"File {fileName} Written Successfully")
37
        else:
38
            print("File writing failed")
39

    
40
    def show_image(self, img, title="Image Title", canvas=None):
41
        ## \breif Function to print image through the projector
42
        # \param img Numpy matrix, loaded image
43
        # \param title Title of the window. 'Image Title' will be the default value when there is no input.
44
        # \paran Canvas parameter needed for library 'Tinker Canvas'. None is default value.
45
        # \return None
46
        img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
47
        img_pil = Image.fromarray(img_rgb)
48
        img_tk = ImageTk.PhotoImage(img_pil)
49
        
50
        canvas.update_idletasks()  # Force the canvas to update its dimensions
51
        canvas_width = canvas.winfo_width()  # Get the updated canvas width
52
        
53
        if self.shared_data.isLeft:
54
            # Align image to the top-right corner
55
            canvas.create_image(canvas_width, 0, anchor=tk.NE, image=img_tk)
56
        else:
57
            # Align image to the top-left corner
58
            canvas.create_image(0, 0, anchor=tk.NW, image=img_tk)
59
        
60
        canvas.image = img_tk  # Prevent image from being garbage collected
61

    
    (1-1/1)