liveBook · Manning

NumPy

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

NumPy is a Python library for numerical computing that provides multidimensional arrays and operations on numerical data.

NumPy provides multidimensional arrays for numerical data. In camera work, OpenCV returns each captured frame as a NumPy ndarray, so image pixels can be inspected, created, and passed directly to OpenCV drawing functions. section 8.3.1 section 8.5

A frame with shape (480, 640, 3) has 480 rows, 640 columns, and three color components per pixel. Captured camera data uses the uint8 data type in this workflow. section 8.3.1

Create a blank image

Import NumPy as np and call np.zeros(shape=(480, 640, 3), dtype=np.uint8) to make a black 640-by-480 BGR image. OpenCV can then draw circles, lines, rectangles, and text onto that array before displaying it. section 8.3.2

Listing 8.1 draw.py: Drawing shapes of different sizes and positions using OpenCV
#!/usr/bin/env python3
import string
import numpy as np
import cv2
 
BLUE = (255, 0, 0)
GREEN = (0, 255, 0)
RED = (0, 0, 255)
 
img = np.zeros(shape=(480, 640, 3), dtype=np.uint8)
 
cv2.circle(img, center=(200, 110), radius=100, color=RED)
cv2.line(img, pt1=(0, 0), pt2=(200, 110), color=GREEN)
cv2.rectangle(img, pt1=(50, 250), pt2=(350, 350), color=BLUE)
 
text = string.ascii_lowercase
font = cv2.FONT_HERSHEY_SIMPLEX
pos = (10, 380)
cv2.putText(img, text, org=pos, fontFace=font, fontScale=1, color=RED)
 
cv2.imshow('preview', img)
cv2.waitKey()

This runnable script creates a zero-filled NumPy image array and uses it as the canvas for OpenCV drawing operations. section 8.3.2

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Demonstrates ndarray type, shape, dtype, and zero-filled image creation without broader NumPy operations.
Examples
Uses a 480×640×3 captured frame and a black array canvas for circles, lines, rectangles, and text.
What it assumes
Assumes Python imports and OpenCV setup while introducing array creation through np.zeros.
What you can do after
Apply it via OpenCV to create and inspect image arrays for camera and drawing workflows.
How it connects
Connects NumPy arrays to OpenCV, camera capture, BGR color encoding, and image drawing.

Also explained in

Compare how 7 books explain NumPy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.