Skip to main content

Pillow

Pillow has more formats, a drawing module, and a large toolbox of its own. libstb decodes, resizes and encodes faster (benchmarks). The two can be used together, and a picture can pass between them in one call. Install the bridge with pip install "libstb[pillow]". Pillow is imported only when you call it.

import os

from PIL import Image as PILImage
from PIL import ImageDraw, ImageFilter

import libstb as stb

os.makedirs("out", exist_ok=True)
img = stb.Image.open("chelsea.png")

# libstb -> Pillow: the mode follows the channel count (L, LA, RGB, RGBA).
pil = img.to_pil()
print("to_pil:", pil.mode, pil.size)
print("gray and alpha:", img.convert(stb.PixelFormat.GRAY).to_pil().mode, img.convert(stb.PixelFormat.RGBA).to_pil().mode)

# L and RGBA are not copied: the Pillow image looks at the pixels of the Image. LA and RGB are copied.
rgba = stb.Image.open("badge.png")
shared = rgba.to_pil()
snapshot = shared.copy() # a copy of the Pillow image is independent
rgba.numpy()[0, 0] = (255, 0, 0, 255)
print("RGBA is shared:", "yes" if shared.getpixel((0, 0)) == (255, 0, 0, 255) else "no")
print("its .copy() is not:", "yes" if snapshot.getpixel((0, 0)) != (255, 0, 0, 255) else "no")

# Pillow -> libstb, with the same pixels.
back = stb.Image.from_pil(pil)
print("from_pil, same pixels:", "yes" if back.tobytes() == img.tobytes() else "no")

# Use each library for what it is good at: libstb for the fast decode, resize and encode, Pillow for the rest of its toolbox.
canvas = img.thumbnail(160, 120).to_pil().filter(ImageFilter.GaussianBlur(1.2))
ImageDraw.Draw(canvas).rectangle((8, 8, 60, 34), outline=(255, 255, 255))
stb.Image.from_pil(canvas).write("out/pillow_round_trip.png")

# from_pil converts the modes that hold 8-bit colors another way, and refuses the rest rather than guess a scale.
for mode in ("P", "1", "CMYK"):
print(f"from_pil {mode}:", stb.Image.from_pil(PILImage.new(mode, (4, 3))).channels, "channels")
try:
stb.Image.from_pil(PILImage.new("F", (4, 3)))
except TypeError:
print("from_pil F: TypeError")
Output
to_pil: RGB (451, 300)
gray and alpha: L RGBA
RGBA is shared: yes
its .copy() is not: yes
from_pil, same pixels: yes
from_pil P: 3 channels
from_pil 1: 1 channels
from_pil CMYK: 3 channels
from_pil F: TypeError
libstb, Pillow blur and rectangle, libstb
libstb, Pillow blur and rectangle, libstb

To Pillow​

img.to_pil() gives a Pillow image whose mode follows the channel count: L for 1, LA for 2, RGB for 3 and RGBA for 4. For L and RGBA the Pillow image looks at the pixels of the Image without a copy, and it is read-only until Pillow first writes to it, when Pillow copies. LA and RGB are copied. If you want a Pillow image that is surely independent, call .copy() on it.

From Pillow​

Image.from_pil(pil) reads a Pillow image. It takes the modes that hold 8-bit colors in a way that has an obvious answer, converting P, 1, CMYK, PA and the like, and refuses the rest, such as F (a float image) or the 16- and 32-bit modes, with a TypeError, because guessing how to scale them would give wrong pictures. Image(pil), the constructor, is stricter still and takes only L, LA, RGB and RGBA, so a palette image is never read as if its indices were colors.

Both directions also work through NumPy's __array_interface__, which Pillow speaks: PILImage.fromarray(img) and stb.Image(pil) need no method of libstb's.

What to use where​

Decode, resize and encode with libstb. Filters that libstb does not have, drawing with Pillow's text and shapes, and formats libstb does not write (WebP, TIFF) are Pillow's. The example blurs and draws with Pillow in the middle of a libstb pipeline and brings the result back.

API at a glance​

  • img.to_pil(), stb.Image.from_pil(pil).
  • stb.Image(pil) for L, LA, RGB and RGBA only.