Skip to content

Instantly share code, notes, and snippets.

@RRMoelker
Last active March 22, 2023 15:37
Show Gist options
  • Save RRMoelker/85fad327b9288bb45bb2ed4b33e871c9 to your computer and use it in GitHub Desktop.
Save RRMoelker/85fad327b9288bb45bb2ed4b33e871c9 to your computer and use it in GitHub Desktop.
Capture luminance still Raspberry PI camera
# python3
# based on https://raspberrypi.stackexchange.com/questions/58871/pi-camera-v2-fast-full-sensor-capture-mode-with-downsampling/58941#58941
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: https://picamera.readthedocs.io/en/release-1.12/recipes2.html#unencoded-image-capture-yuv-format
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
resolution=RESOLUTION,
framerate=40
) 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)
try:
camera.capture(y_data, 'yuv') # YUV420
except IOError:
pass
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
print('saving...')
im.save('test.jpg')
im.save('test.bmp')
print('done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment