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

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.

CameraWebServe: ESP32-S3 (arduino-esp32) + OV5640 camera module