Represent RGB images as 3D (Height, Width, 3) arrays, crop, tint, and grayscale.
Every digital image is simply a 3D NumPy array of shape `(Height, Width, Channels)`. Color channels are `0: Red`, `1: Green`, and `2: Blue` with pixel values from 0 to 255.
An image is a giant mosaic grid of colored tiles. Cropping an image is basic array slicing `img[100:200, 100:200]`. Inverting an image is `255 - img`.
`img.shape` = `(Height, Width, 3)`. Data type is `np.uint8` (0 to 255 unsigned 8-bit integers).
gray = (img * [0.299, 0.587, 0.114]).sum(axis=-1)img = img + 50 # uint8 overflow: 250 + 50 wraps around to 44!img = np.clip(img.astype(np.int32) + 50, 0, 255).astype(np.uint8)Adding numbers directly to `uint8` wraps around 255 to 0. Always cast to int32, add, clip to 255, then cast back.
Solve the objective below using NumPy vectorized syntax
Given pixel values `p = np.array([0, 100, 255], dtype=np.uint8)`, invert them with `255 - p` and print.
[255 155 0]