Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Created November 16, 2025 17:18
Show Gist options
  • Select an option

  • Save maxpromer/3cdf718647b16d0dfc009ffa1f15add6 to your computer and use it in GitHub Desktop.

Select an option

Save maxpromer/3cdf718647b16d0dfc009ffa1f15add6 to your computer and use it in GitHub Desktop.
Raspberry Pi Camera Module 3 image preview with Python
from picamera2 import Picamera2
import cv2
# Initialize the camera
picam2 = Picamera2()
picam2.start()
try:
while True:
# Capture frame from the camera
frame = picam2.capture_array()
# Convert color from RGB to BGR for OpenCV
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
# Display the frame
cv2.imshow("Camera Feed", frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
# Close all OpenCV windows
cv2.destroyAllWindows()
# Stop the camera
picam2.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment