Skip to content

Commit

Permalink
Deprecate use of the ScalerCropMaximum property
Browse files Browse the repository at this point in the history
This will be dropped by libcamera. Instead, use the maximum value of
the ScalerCrop control, which stores an identical rectangle.

Signed-off-by: Naushir Patuck <[email protected]>
  • Loading branch information
naushir committed Mar 1, 2024
1 parent 6348fcd commit 1e1b1c2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
21 changes: 11 additions & 10 deletions apps/app_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
QTabWidget, QVBoxLayout, QWidget)

from picamera2 import Picamera2
from picamera2 import utils
from picamera2.encoders import H264Encoder, Quality
from picamera2.outputs import FfmpegOutput, FileOutput
from picamera2.previews.qt import QGlPicamera2
Expand Down Expand Up @@ -92,7 +93,7 @@ def update_controls():
global scaler_crop

# Fix aspect ratio of the pan/zoom
full_img = picam2.camera_properties["ScalerCropMaximum"]
full_img = utils.convert_from_libcamera_type(picam2.camera_ctrl_info['ScalerCrop'][1].max)
ar = full_img[2] / full_img[3]
new_scaler_crop = list(scaler_crop)
new_scaler_crop[3] = int(new_scaler_crop[2] / ar)
Expand Down Expand Up @@ -516,7 +517,7 @@ class panZoomDisplay(QWidget):
def __init__(self):
super().__init__()
self.setMinimumSize(201, 151)
self.scale = 200 / picam2.camera_properties["ScalerCropMaximum"][2]
self.scale = 200 / utils.convert_from_libcamera_type(picam2.camera_ctrl_info['ScalerCrop'][1].max)[2]
self.zoom_level_ = 1.0
self.max_zoom = 7.0
self.zoom_step = 0.1
Expand All @@ -537,7 +538,7 @@ def setZoomLevel(self, val):
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
full_img = picam2.camera_properties["ScalerCropMaximum"]
full_img = utils.convert_from_libcamera_type(picam2.camera_ctrl_info['ScalerCrop'][1].max)
self.scale = 200 / full_img[2]
# Whole frame
scaled_full_img = [int(i * self.scale) for i in full_img]
Expand All @@ -555,11 +556,11 @@ def paintEvent(self, event):
def draw_centered(self, pos):
global scaler_crop
center = [int(i / self.scale) for i in pos]
full_img = picam2.camera_properties["ScalerCropMaximum"]
full_img = utils.convert_from_libcamera_type(picam2.camera_ctrl_info['ScalerCrop'][1].max)
w = scaler_crop[2]
h = scaler_crop[3]
x = center[0] - w // 2 + picam2.camera_properties["ScalerCropMaximum"][0]
y = center[1] - h // 2 + picam2.camera_properties["ScalerCropMaximum"][1]
x = center[0] - w // 2 + full_img[0]
y = center[1] - h // 2 + full_img[1]
new_scaler_crop = [x, y, w, h]

# Check still within bounds
Expand All @@ -583,7 +584,7 @@ def setZoom(self):
if self.zoom_level > self.max_zoom:
self.zoom_level = self.max_zoom
factor = 1.0 / self.zoom_level
full_img = picam2.camera_properties["ScalerCropMaximum"]
full_img = utils.convert_from_libcamera_type(picam2.camera_ctrl_info['ScalerCrop'][1].max)
current_center = (scaler_crop[0] + scaler_crop[2] // 2, scaler_crop[1] + scaler_crop[3] // 2)
w = int(factor * full_img[2])
h = int(factor * full_img[3])
Expand Down Expand Up @@ -1128,9 +1129,9 @@ def apply_settings(self):
# Finally set the modes and check sensor crop
if self.preview_check.isChecked():
switch_config("still")
current_crop = picam2.camera_properties["ScalerCropMaximum"]
current_crop = utils.convert_from_libcamera_type(picam2.camera_ctrl_info['ScalerCrop'][1].max)
switch_config("preview")
if current_crop != picam2.camera_properties["ScalerCropMaximum"]:
if current_crop != utils.convert_from_libcamera_type(picam2.camera_ctrl_info['ScalerCrop'][1].max):
print("Preview and Still configs have different aspect ratios")
self.preview_warning.show()
else:
Expand Down Expand Up @@ -1208,7 +1209,7 @@ def toggle_hidden_controls():
# Final setup
window.setWindowTitle("Qt Picamera2 App")
recording = False
scaler_crop = picam2.camera_properties["ScalerCropMaximum"]
scaler_crop = utils.convert_from_libcamera_type(picam2.camera_ctrl_info['ScalerCrop'][1].max)
hdr_imgs = {"exposures": None}
pic_tab.apply_settings()

Expand Down
2 changes: 1 addition & 1 deletion picamera2/picamera2.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ def sensor_modes(self) -> list:
self.configure(temp_config)
frameDurationMin = self.camera_controls["FrameDurationLimits"][0]
cam_mode["fps"] = round(1e6 / frameDurationMin, 2)
cam_mode["crop_limits"] = self.camera_properties["ScalerCropMaximum"]
cam_mode["crop_limits"] = utils.convert_from_libcamera_type(self.camera_ctrl_info['ScalerCrop'][1].max)
cam_mode["exposure_limits"] = tuple([i for i in self.camera_controls["ExposureTime"] if i != 0])
self.sensor_modes_.append(cam_mode)
return self.sensor_modes_
Expand Down
3 changes: 2 additions & 1 deletion tests/autofocus_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from libcamera import controls

from picamera2 import Picamera2
from picamera2 import utils

picam2 = Picamera2()
if 'AfMode' not in picam2.camera_controls:
Expand Down Expand Up @@ -94,6 +95,6 @@
time.sleep(0.1)

print("Test AfWindows")
max_window = picam2.camera_properties['ScalerCropMaximum']
max_window = utils.convert_from_libcamera_type(picam2.camera_ctrl_info['ScalerCrop'][1].max)
picam2.set_controls({'AfWindows': [max_window]})
time.sleep(0.1)

0 comments on commit 1e1b1c2

Please sign in to comment.