Skip to content

Commit

Permalink
Fixes for cameras with no full sensor resolution modes
Browse files Browse the repository at this point in the history
We don't want to advertise Picamera2's "sensor_resolution" as being
larger than the largest available camera mode. This does require a
couple of tweaks when elsewhere, for example when using the ScalerCrop
control, because this still works in units of the full pixel array.

Signed-off-by: David Plowman <[email protected]>
  • Loading branch information
davidplowman committed Jan 5, 2023
1 parent cf2a50a commit 6a884ca
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
4 changes: 3 additions & 1 deletion examples/zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

size = picam2.capture_metadata()['ScalerCrop'][2:]

full_res = picam2.camera_properties['PixelArraySize']

for _ in range(20):
# This syncs us to the arrival of a new camera frame:
picam2.capture_metadata()

size = [int(s * 0.95) for s in size]
offset = [(r - s) // 2 for r, s in zip(picam2.sensor_resolution, size)]
offset = [(r - s) // 2 for r, s in zip(full_res, size)]
picam2.set_controls({"ScalerCrop": offset + size})

time.sleep(2)
7 changes: 4 additions & 3 deletions picamera2/picamera2.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,10 @@ def _initialize_camera(self) -> bool:
for k, v in self.camera.properties.items():
self.camera_properties_[k.name] = convert_from_libcamera_type(v)

# The next two lines could be placed elsewhere?
self.sensor_resolution = self.camera_properties_["PixelArraySize"]
self.sensor_format = str(self.camera.generate_configuration([RAW]).at(0).pixel_format)
# These next lines could be placed elsewhere?
raw_mode = self.camera.generate_configuration([RAW]).at(0)
self.sensor_resolution = (raw_mode.size.width, raw_mode.size.height)
self.sensor_format = str(raw_mode.pixel_format)

_log.info('Initialization successful.')
return True
Expand Down
2 changes: 1 addition & 1 deletion tests/configurations.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
picam2.still_configuration.enable_lores()
picam2.still_configuration.lores.format = "YUV420"
picam2.still_configuration.enable_raw()
half_res = tuple([v // 2 for v in picam2.sensor_resolution])
half_res = tuple([v // 2 for v in picam2.camera_properties['PixelArraySize']])
picam2.still_configuration.raw.size = half_res

picam2.configure("preview")
Expand Down

0 comments on commit 6a884ca

Please sign in to comment.