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
#6
If you just want to display images, you can use tkinter.PhotoImage() to load the image.
import tkinter as tk
from tkinter import filedialog
 
class Application(tk.Tk):
    def __init__(self, image_path=None):
        super().__init__()
        self.title("Image Processing")
        self.geometry("400x300")
        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:
            self.image = tk.PhotoImage(file=filename)
            self.display["image"] = self.image
 
    def save_image(self):
        """Save image to a file"""
        filename = filedialog.asksaveasfilename(defaultextension='.jpg')
        if filename:
            self.image.write(filename)
 
Application().mainloop()
If you add image processing to your image processing application, you'll probably want to use PIL. You'll save the image as a PIL image for doing manipulations. Each time you modify the image you'll need to convert it to a tkImage and display.

Do not create a label each time you change the image. Use the same label over and over. Each time you change the image, change the image attribute for the label.

Not everything needs to be an attribute of your class. For example, I create three buttons, but my application class doesn't save handles to the buttons. If you don't need to keep it, don't keep it. I save the display label because that is used later in the program. I keep the image, because you need to do that to have the image display. I do not keep the buttons or the button form. I never refence those objects after they are created.

There are a lot of people who write tkinter programs as you did, with a class that implements the logic, but is not a tkinter object. I think that is a poor design. In the example above, I made Application a subclass of tk.Tk, the root window. This makes Applicaltion automatically know how to do everything that root knows how to do. It also cleans up all the "self.window.blah-blah-blah".

Do not use canvas unless you want to draw shapes or make a scrolled window. In tkinter you should make widgets that are children of a window and use a layout manager (pack or grid) to position the controls in the window. Place is hardly ever used, except for windows created using a designer tool. Properly used, pack and grid make windows that resize nicely. It is hard to do that using place.
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 751 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  [Tkinter] image in label not showing? rwahdan 2 8,298 Jun-25-2021, 10:27 AM
Last Post: rwahdan
  tkinter: Image to Label Maryan 10 5,384 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  [Tkinter] Returning always the last image into Label Lucas_Ribeiro 1 2,149 May-08-2020, 05:56 PM
Last Post: deanhystad
  Refresh image in label after every 1s using simple function jenkins43 1 5,521 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  [Tkinter] Please help, my Label does not show up on my gui ? robertinoc 2 2,715 Jun-12-2019, 05:58 PM
Last Post: robertinoc
  [Tkinter] Image does not show in treeview. KevinBrown 3 7,612 May-05-2019, 11:47 PM
Last Post: KevinBrown
  Can't load a png image tkinter Pythenx 2 10,093 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