Python script with tkinter GUI to capture images from Raspberry Pi Camera Module 3

Python script with tkinter GUI run on Raspberry Pi 4B/8G running 32-bit Raspberry Pi OS (bullseye), to capture images from Raspberry Pi Camera Module 3.


cam_tkinter_capture.py
from tkinter import *
from tkinter import messagebox
from picamera2 import Picamera2, Preview
from libcamera import controls
import time
import sys

picam2 = Picamera2()
camera_config = picam2.create_preview_configuration()
picam2.configure(camera_config)
picam2.start_preview(Preview.QTGL)
picam2.set_controls({"AfMode": controls.AfModeEnum.Continuous})
picam2.start()

root = Tk()
root.title(sys.argv[0])
root.geometry("550x450")
# center on screen
root.eval('tk::PlaceWindow . center')

labelframe = LabelFrame(root, text="Click button to capture image")
labelframe.pack(fill = "both", expand = "yes")

def btnCallBack():
    print("- Capture image -")
    timeStamp = time.strftime("%Y%m%d-%H%M%S")
    targetPath="/home/pi/Desktop/img_"+timeStamp+".jpg"
    picam2.capture_file(targetPath)
    print("image save:", targetPath)
    print("----------------------------------------------------")

B = Button(labelframe, text ="Capture", command = btnCallBack)

B.pack()

def on_closing():
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        picam2.stop_preview()
        root.destroy()
        
root.protocol("WM_DELETE_WINDOW", on_closing)

root.mainloop()

next:
~ with Toggling AF Mode function added.

Exercises run on Raspberry Pi 5 running 64-bit Raspberry Pi OS (bookworm) with Camera Module 3:
Using the Raspberry Pi Camera in Python3/PyQt5 applications using picamera2 lib


Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

Drive 320x240 ILI9341 SPI TFT using ESP32-S3 (NodeMCU ESP-S3-12K-Kit) using TFT_eSPI library, in Arduino Framework.