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
#8
A program that reads and writes image files using opencv and displays them in tkinter.
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import cv2

class Application(tk.Tk):
    def __init__(self, image_path=None):
        super().__init__()
        self.title("Image Processing")
        self.geometry("400x300")
    
        self.image = None   # Image used for image processing
        self.tkimage = None # Image used to display in tkinter label
    
        self.display = tk.Label(self)
        self.display.pack(expand=True, fill=tk.BOTH)

        # Make a frame to pack the buttons horizontally
        buttons = tk.Frame(self)
        buttons.pack(side=tk.BOTTOM, fill=tk.X, padx=5, pady=5)

        # Make buttons to load image, save image and quit
        button = tk.Button(buttons, text="LOAD IMAGE", command=self.load_image)
        button.pack(side=tk.LEFT, expand=True, fill=tk.X)
        button = tk.Button(buttons, text="SAVE FILE", command=self.save_image)
        button.pack(side=tk.LEFT, expand=True, fill=tk.X, padx=5)
        button = tk.Button(buttons, text="CLOSE", command=self.destroy)
        button.pack(side=tk.LEFT, expand=True, fill=tk.X)

    def load_image(self):
        """Select an image to display"""
        filename = filedialog.askopenfilename()
        if filename:
            # Read image and convert to RGB
            self.image = cv2.imread(filename)
            self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)

            # Convert image to tkinter image format and display
            self.tkimage = ImageTk.PhotoImage(Image.fromarray(self.image))
            self.display["image"] = self.tkimage

    def save_image(self):
        """Save image to a file"""
        filename = filedialog.asksaveasfilename(defaultextension='.jpg')
        if filename:
            # Convert image to BGR and write to file.
            cv2.imwrite(filename, cv2.cvtColor(self.image, cv2.COLOR_RGB2BGR))

Application().mainloop()
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 3,151 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  [Tkinter] image in label not showing? rwahdan 2 11,343 Jun-25-2021, 10:27 AM
Last Post: rwahdan
  tkinter: Image to Label Maryan 10 7,219 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  [Tkinter] Returning always the last image into Label Lucas_Ribeiro 1 2,671 May-08-2020, 05:56 PM
Last Post: deanhystad
  Refresh image in label after every 1s using simple function jenkins43 1 6,139 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  [Tkinter] Please help, my Label does not show up on my gui ? robertinoc 2 3,497 Jun-12-2019, 05:58 PM
Last Post: robertinoc
  [Tkinter] Image does not show in treeview. KevinBrown 3 9,023 May-05-2019, 11:47 PM
Last Post: KevinBrown
  Can't load a png image tkinter Pythenx 2 11,010 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