How to use picamera2 to capture Raspberry Pi HQ camera (IMX477) high resolution image

The following code will capture a single 4056x3056px image from a Raspberry Pi HQ camera using the IMX477 sensor into either a file or a numpy array. This is the maximum resolution supported by that camera.

Capturing to a file

This will capture a single frame to CameraTest.png. The PNG file will be quite large, around 15-25 Megabytes.

#!/usr/bin/env python3
import time
import picamera2
import numpy as np

with picamera2.Picamera2() as camera:
    # Create high resolution still capture config
    camera_config = camera.create_still_configuration({"size":(4056, 3040)})
    camera.configure(camera_config)
    # Start camera with config
    camera.start()
    # Capture a single frame to CameraTest.png
    camera.capture_file("CameraTest.png")

Capturing to a numpy array

#!/usr/bin/env python3
import time
import picamera2
import numpy as np

with picamera2.Picamera2() as camera:
    # Create high resolution still capture config
    camera_config = camera.create_still_configuration({"size":(4056, 3040)})
    camera.configure(camera_config)
    # Start capture
    camera.start()
    array = camera.capture_array("main")
    # TODO Do something with [array]
    print(array.shape)

Example output:

[0:55:52.878964095] [5768]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
[0:55:52.913906171] [5769]  INFO RPI vc4.cpp:437 Registered camera /base/soc/i2c0mux/i2c@1/imx477@1a to Unicam device /dev/media3 and ISP device /dev/media0
[0:55:52.913998855] [5769]  INFO RPI pipeline_base.cpp:1101 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:55:52.921056014] [5768]  INFO Camera camera.cpp:1033 configuring streams: (0) 4056x3040-BGR888 (1) 4056x3040-SBGGR12_CSI2P
[0:55:52.922090144] [5769]  INFO RPI vc4.cpp:565 Sensor: /base/soc/i2c0mux/i2c@1/imx477@1a - Selected sensor format: 4056x3040-SBGGR12_1X12 - Selected unicam format: 4056x3040-pBCC
(3040, 4056, 3)

Note that the NumPy array is compatible with Python OpenCV functions.