DLPack
DLPack is the standard way for array libraries to hand a block of memory to each other without copying it: NumPy, PyTorch, JAX and CuPy all speak it. An Image is a DLPack object, so numpy.from_dlpack(img), torch.from_dlpack(img) and jax.numpy.from_dlpack(img) give you the pixels directly. This page is only for Python.
import os
import numpy as np
import libstb as stb
os.makedirs("out", exist_ok=True)
img = stb.Image.open("chelsea.png").thumbnail(96, 64)
# Image -> framework. The layout is fixed: uint8, (height, width, channels), on the CPU.
view = np.from_dlpack(img)
print("numpy view:", view.shape, view.dtype)
print("shares memory:", "yes" if np.shares_memory(view, img.numpy()) else "no")
# A model usually wants (channels, height, width) floats between 0 and 1: do that on the framework side.
chw = view.transpose(2, 0, 1).astype(np.float32) / 255
print("chw float32:", chw.shape, chw.dtype, float(chw.max()))
# torch.from_dlpack(img) and jax.numpy.from_dlpack(img) take the same call.
# Framework -> Image: any DLPack object holding uint8 (H, W) or (H, W, 1 to 4) pixels on the CPU.
restored = np.ascontiguousarray((chw * 255).round().astype(np.uint8).transpose(1, 2, 0))
back = stb.Image.from_dlpack(restored)
print("round trip exact:", "yes" if back.tobytes() == img.tobytes() else "no")
back.write("out/roundtrip.png")
# Something that is not a DLPack object is a clear error.
try:
stb.Image.from_dlpack(b"not a tensor")
except TypeError:
print("not a DLPack object: TypeError")
numpy view: (64, 96, 3) uint8
shares memory: yes
chw float32: (3, 64, 96) float32 0.8156862854957581
round trip exact: yes
not a DLPack object: TypeError

The layout
The layout is fixed: uint8, shape (height, width, channels), on the CPU. libstb does not guess what a model wants. A network usually takes (channels, height, width) floats between 0 and 1, and that conversion belongs on the framework's side, where it is one line:
chw = torch.from_dlpack(img).permute(2, 0, 1).float() / 255
Sharing and copying
The view and the image are the same memory, so a change on one side is seen on the other. A framework that imports DLPack read-only (older NumPy does) gives you a read-only view. NumPy 2.1 and later also accept from_dlpack(img, copy=True) for an independent array, and so do PyTorch and JAX.
Coming back
Image.from_dlpack(obj) takes any DLPack object that holds uint8 pixels of shape (H, W) or (H, W, 1 to 4) on the CPU. A writable, C-contiguous array is shared and anything else, a read-only array or a strided one, is copied. A model's output is usually (C, H, W) floats, so go back to uint8 (H, W, C) first, for instance (t.clamp(0, 1) * 255).byte().permute(1, 2, 0).contiguous(). An object that is not a DLPack one is a TypeError.
API at a glance
numpy.from_dlpack(img),torch.from_dlpack(img),jax.numpy.from_dlpack(img),cupy.from_dlpack(img).stb.Image.from_dlpack(obj).