Skip to main content

NumPy

The Python package has no NumPy dependency, and works without it. With NumPy installed (pip install "libstb[numpy]"), an Image and an array can be the same memory, and a set of functions takes and returns arrays: imread, imwrite, imresize, and every filter and tone function. This page is only for Python. In C++, an image's pixels are img.data(), and stb::image::wrap builds an image around pixels you already hold.

import os

import numpy as np

import libstb as stb

os.makedirs("out", exist_ok=True)

# A writable, C-contiguous uint8 array is used in place by Image(array): no copy.
arr = np.zeros((4, 4, 3), dtype=np.uint8)
img = stb.Image(arr)
arr[0, 0] = (255, 0, 0)
print("a write to the array shows in the image:", img.numpy()[0, 0].tolist())
img.numpy()[1, 1] = (0, 255, 0)
print("a write to the image shows in the array:", arr[1, 1].tolist())

# An array that cannot be shared, read-only or strided, is copied instead.
flipped = stb.Image(arr[::-1])
arr[3, 3] = (9, 9, 9)
print("a strided array is a copy:", "yes" if tuple(flipped.numpy()[0, 0]) != (9, 9, 9) else "no")

# When you want an image of its own, say so.
clone = img.copy()
arr[2, 2] = (7, 7, 7)
print("copy() is independent:", "yes" if tuple(clone.numpy()[2, 2]) == (0, 0, 0) else "no")

# imread, imwrite and imresize are the same operations with arrays in and arrays out.
pixels = stb.imread("chelsea.png")
print("imread:", pixels.shape, pixels.dtype)
stb.imwrite("out/q30.jpg", pixels, quality=30) # the format comes from the extension
small = stb.imresize(pixels, 160, 120)
print("imresize:", small.shape)
stb.imwrite("out/small.png", small)

# An option the format does not have is an error, never ignored.
try:
stb.imwrite("out/bad.png", pixels, quality=30)
except ValueError:
print("quality for a png: ValueError")
Output
a write to the array shows in the image: [255, 0, 0]
a write to the image shows in the array: [0, 255, 0]
a strided array is a copy: yes
copy() is independent: yes
imread: (300, 451, 3) uint8
imresize: (120, 160, 3)
quality for a png: ValueError
imresize(160, 120)
imresize(160, 120)

Sharing​

Image(array) takes a writable, C-contiguous uint8 array of shape (height, width) or (height, width, 1 to 4) and uses it in place. A write on either side is seen on the other, because there is one block of memory. img.numpy() gives the same view the other way.

A read-only array or a strided one (a slice with a step, a flipped view) cannot be shared. It is copied, and the example shows that a later write to the array does not reach the image. img.copy() is the explicit way to get an image of your own.

The memory belongs to whoever created it. The Image keeps the array alive for as long as the image lives, so there is no way to use the pixels after they are freed.

Functions that take arrays​

imread(path) returns a uint8 array (H, W, C) and takes the same options as Image.open (format, flip, exif_orient, max_bytes). imwrite(path, array, **options) picks the format from the extension, and an option that the format does not have is a ValueError: quality is for JPEG, compression for PNG, rle for TGA. imresize(array, width, height) resizes an array and returns an array.

Many functions of the library take a uint8 array or a float32 array (0 to 1) of shape (H, W) or (H, W, C) in place of an Image and give the same kind back. A function that does not understand what it is given says TypeError.

Without NumPy, memoryview(img) is a zero-copy three-dimensional view, img.tobytes() is a copy, and any buffer (a bytearray, an array.array, a memoryview) can be the input of Image(...).

API at a glance​

  • stb.Image(array) shares a writable C-contiguous uint8 array, and copies any other.
  • img.numpy() is a writable view of the pixels, shape (H, W, C).
  • stb.imread(path, **options), stb.imwrite(path, array, **options), stb.imresize(array, width, height).
  • img.copy() is an independent image.