from picamera2 import Picamera2 import cv2 def main(): picam2 = Picamera2() picam2.configure(picam2.create_preview_configuration(main={"format": "BGR888", "size": (640, 480)})) picam2.start() x, y, w, h = [100, 0, 260, 450] print("Type 's' to save a still image. Type 'q' to quit.") while True: cmd = input("Command [s/q]: ").strip().lower() if cmd == 's': frame = picam2.capture_array() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frame = cv2.equalizeHist(frame) # Draw a green rectangle on the image cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.imwrite("still_image.jpg", frame) print("Saved still_image.jpg") elif cmd == 'q': break picam2.stop() if __name__ == "__main__": main()