Oct-23-2022, 10:21 AM
Hello,
I'm new to Tkinter and Python and trying to display an image in a label by selecting from a folder.
I'm really thankful for any suggestions!
I'm new to Tkinter and Python and trying to display an image in a label by selecting from a folder.
from asyncore import read from email.mime import image import tkinter as tk from tkinter import filedialog from turtle import width import cv2 as cv from PIL import Image from PIL import ImageTk from io import BytesIO class Application: def __init__(root, window, window_title, image_path): root.window = window root.window.title(window_title) root.image_path = image_path root.cv_img = cv.cvtColor(cv.imread(image_path), cv.COLOR_BGR2RGB) root.height, root.width, no_channels = root.cv_img.shape root.canvas = tk.Canvas(window, width=500, height=700, bg='azure3', relief='raised') root.img_show = ImageTk.PhotoImage(image=Image.fromarray(root.cv_img)) root.headline_label = tk.Label(text="IMAGE PROCESSING", bg='azure3') root.up_window_label = tk.Label(image=root.img_show) root.down_window_label = tk.Label(bg='white') root.load_button = tk.Button(text="LOAD IMAGE", command=root.load_image) root.save_button = tk.Button(text="SAVE FILE", command=root.save_image) root.close_button = tk.Button(text="CLOSE", command=root.window.destroy) root.canvas.create_window(80, 20, window=root.headline_label) root.canvas.create_window(root.width*0.7, root.height*0.7, window=root.up_window_label) root.canvas.create_window(100, 600, window=root.load_button) root.canvas.create_window(250, 600, window=root.save_button) root.canvas.create_window(400, 600, window=root.close_button) root.canvas.pack() root.window.mainloop() def load_image(root): global img_show root.import_file_path = filedialog.askopenfilename() root.new_img = Image.open(root.import_file_path) root.pic = ImageTk.PhotoImage(image=Image.fromarray(root.new_img)) root.down_window_label = tk.Label(image=root.pic) root.canvas.create_window(root.width*0.7, root.height*2, window=root.down_window_label) def save_image(root): global img_show root.export_file_path = filedialog.asksaveasfilename(defaultextension='.jpg') root.img_show.save(root.export_file_path) Application(tk.Tk(), "IMAGE_PROCESSING_APPLICATION", "OpenCV_Logo.jpeg")After loading the image by clicking the button, this error is given:
Error:TypeError: a bytes-like object is required, not 'JpegImageFile'
It may has to be solved with BytesIO, but I can't find something what helps me.I'm really thankful for any suggestions!