Skip to content

Instantly share code, notes, and snippets.

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

  • Save maxpromer/7e14269d2c5dc59618d9c610762d2cbe to your computer and use it in GitHub Desktop.

Select an option

Save maxpromer/7e14269d2c5dc59618d9c610762d2cbe to your computer and use it in GitHub Desktop.
Raspberry Pi Camera Module 3 image editor by OpenCV
from picamera2 import Picamera2
import cv2
picam2 = Picamera2()
# Set up Auto Focus
config = picam2.create_preview_configuration()
picam2.configure(config)
picam2.start()
# Enable Continuous Auto Focus
picam2.set_controls({"AfMode": 2, "AfTrigger": 0})
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)
# Resize image to 50%
frame_resized = cv2.resize(frame, None, fx=0.5, fy=0.5)
# Draw rectangle
cv2.rectangle(frame_resized, (50, 50), (150, 100), (0, 0, 255), 2)
# Add text to image
cv2.putText(frame_resized, "Hello, OpenCV !", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
# Display the frame
cv2.imshow("OpenCV Draw", frame_resized)
# 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