It sounds like your Raspberry Pi is hanging or freezing right when cv2.imshow()
is called — the window shows up black, small, and the system becomes unresponsive. That's definitely not normal OpenCV behavior, especially for a simple image display.
Here’s a breakdown of what could be going wrong and how to fix or debug it:
OpenCV’s imshow()
relies on a GUI environment (like X11 on Linux). If your Raspberry Pi is:
Running headless (no monitor or desktop GUI)
Not running in a graphical session (like LXDE/X11)
Or your script is being launched in a virtual terminal or via SSH
Then imshow()
has no display to show the window, and trying to do so can cause serious issues (including freezes).
Make sure you're running your script from a desktop environment on the Pi:
NOT via SSH unless X11 forwarding is set up.
If headless, consider using matplotlib or saving the image to a file instead.
Before running your full function, test this:
import cv2
import numpy as np
img = np.zeros((300, 600, 3), dtype=np.uint8)
cv2.imshow("Test", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
If even this causes the Pi to hang or show a black window → the issue is with the OpenCV GUI backend setup.
cv2.namedWindow()
explicitlyTry specifying the window type before imshow()
:
cv2.namedWindow("Confirm Images (Front & Back)", cv2.WINDOW_NORMAL)
cv2.imshow("Confirm Images (Front & Back)", combined_img)
This gives more control over how OpenCV creates the window and can prevent weird size or render bugs.
The cv2.moveWindow()
call may cause issues on limited environments. Try commenting this out:
# cv2.moveWindow("Confirm Images (Front & Back)", 1, 1)
It’s not needed unless you know the Pi's screen resolution and want precise placement.
waitKey(0)
in non-interactive environmentsInstead of waiting forever for a key press, try a timeout:
key = cv2.waitKey(5000) # wait 5 seconds
if key == ord('y'):
print("Images confirmed.")
elif key == ord('n'):
print("Retaking images...")
else:
print("No key pressed. Defaulting to confirm.")
Before calling imshow()
, print out image shapes and check for None
:
print(f"Front image shape: {front_img.shape}")
print(f"Back image shape: {back_img.shape}")
This confirms the images loaded and resized properly.
If OpenCV GUI keeps failing, save and preview:
cv2.imwrite("combined.jpg", combined_img)
print("Saved combined image. Please open 'combined.jpg' manually to confirm.")