How to capture image as NumPy array using PiCamera2

This will capture a raspberry pi camera image as numpy array.

The default size that will be used is 640x480px

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

with picamera2.Picamera2() as camera:
    camera.start()
    time.sleep(1)
    array = camera.capture_array("main")
    # TODO Do something with array
    print(array.shape)

Example output:

[0:27:57.224504277] [3117]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
[0:27:57.258472502] [3118]  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:27:57.258611296] [3118]  INFO RPI pipeline_base.cpp:1101 Using configuration file '/usr/share/libcamera/pipeline/rpi/vc4/rpi_apps.yaml'
[0:27:57.264790966] [3117]  INFO Camera camera.cpp:1033 configuring streams: (0) 640x480-XBGR8888 (1) 2028x1520-SBGGR12_CSI2P
[0:27:57.265395993] [3118]  INFO RPI vc4.cpp:565 Sensor: /base/soc/i2c0mux/i2c@1/imx477@1a - Selected sensor format: 2028x1520-SBGGR12_1X12 - Selected unicam format: 2028x1520-pBCC
(480, 640, 4)