Python Forum
[Tkinter] Load image and show in label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Load image and show in label
#1
Hello,
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!
Reply


Messages In This Thread
Load image and show in label - by ko_fal - Oct-23-2022, 10:21 AM
RE: Load image and show in label - by Larz60+ - Oct-23-2022, 11:47 AM
RE: Load image and show in label - by ko_fal - Oct-23-2022, 11:56 AM
RE: Load image and show in label - by joe_momma - Oct-23-2022, 06:59 PM
RE: Load image and show in label - by ko_fal - Oct-23-2022, 10:04 PM
RE: Load image and show in label - by deanhystad - Oct-24-2022, 03:20 AM
RE: Load image and show in label - by ko_fal - Oct-24-2022, 12:26 PM
RE: Load image and show in label - by deanhystad - Oct-24-2022, 06:20 PM
RE: Load image and show in label - by ko_fal - Oct-25-2022, 09:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 1,097 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  [Tkinter] image in label not showing? rwahdan 2 8,597 Jun-25-2021, 10:27 AM
Last Post: rwahdan
  tkinter: Image to Label Maryan 10 5,511 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  [Tkinter] Returning always the last image into Label Lucas_Ribeiro 1 2,218 May-08-2020, 05:56 PM
Last Post: deanhystad
  Refresh image in label after every 1s using simple function jenkins43 1 5,556 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  [Tkinter] Please help, my Label does not show up on my gui ? robertinoc 2 2,778 Jun-12-2019, 05:58 PM
Last Post: robertinoc
  [Tkinter] Image does not show in treeview. KevinBrown 3 7,740 May-05-2019, 11:47 PM
Last Post: KevinBrown
  Can't load a png image tkinter Pythenx 2 10,169 May-04-2019, 05:43 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020