To display the latest captured image and show it in a label using Tkinter, you can follow these steps:

  1. Import the necessary modules:
from PIL import ImageTk, Image
import tkinter as tk
import os
  1. Create a Tkinter window and a label to display the image:
window = tk.Tk()
label = tk.Label(window)
  1. Define a function to update the image:
def update_image():
    # Get the path of the latest captured image
    latest_image = max(os.listdir('path_to_directory'), key=os.path.getctime)

    # Open the image using PIL
    image ='path_to_directory/' + latest_image)

    # Resize the image if necessary
    # image = image.resize((desired_width, desired_height))

    # Convert the image to Tkinter-compatible format
    image_tk = ImageTk.PhotoImage(image)

    # Update the label with the new image
    label.image = image_tk  # Keep a reference to prevent garbage collection
  1. Call the update_image function to initially display the latest captured image:
  1. Use a timer or an event to periodically call the update_image function to check for new images and update the label:
window.after(1000, update_image)  # Update image every second (adjust the delay as needed)
  1. Run the Tkinter event loop:

Make sure to replace 'path_to_directory' with the actual path to the directory where your captured images are stored. Uncomment and adjust the code related to image resizing if needed.

This code will continuously check for the latest captured image in the specified directory and update the label with the new image.

Leave A Comment