Skip to content

Instantly share code, notes, and snippets.

Last active March 22, 2023 15:37
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Capture luminance still Raspberry PI camera
# python3
# based on
import time
import picamera
import numpy as np
from PIL import Image
RESOLUTION = (1640, 1232)
# Calculate the actual image size in the stream (accounting for rounding
# of the resolution)
# Capturing yuv will round horizontal resolution to 16 multiple and vertical to 32 multiple
# see:
fwidth = (RESOLUTION[0] + 31) // 32 * 32
fheight = (RESOLUTION[1] + 15) // 16 * 16
print(f'frame size {fwidth}x{fheight}')
with picamera.PiCamera(
sensor_mode=4, # 1640x1232, full FoV, binning 2x2
) as camera:
print('camera setup')
camera.rotation = 180
time.sleep(2) # let the camera warm up and set gain/white balance
print('starting capture')
y_data = np.empty((fheight, fwidth), dtype=np.uint8)
camera.capture(y_data, 'yuv') # YUV420
except IOError:
y_data = y_data[:RESOLUTION[1], :RESOLUTION[0]] # crop numpy array to RESOLUTION
# y_data now contains the Y-plane only
print('convert to Pillow image')
im = Image.fromarray(y_data, mode='L') # using luminance mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment