Encode and write
libstb writes PNG, JPEG, BMP and TGA. Each format has a to_* call that returns the bytes of the file, for a socket, a database or a test, and a write_* call that writes the file. write chooses the format from the extension of the path.
import os
import libstb as stb
os.makedirs("out", exist_ok=True)
img = stb.Image.open("chelsea.png")
# to_* returns the bytes of a file; each format has its own setting.
print(f"png, compression 1: {len(img.to_png(compression=1))} bytes")
print(f"png, compression 9: {len(img.to_png(compression=9))} bytes")
for quality in (20, 60, 90):
print(f"jpg, quality {quality}: {len(img.to_jpg(quality=quality))} bytes")
print(f"bmp: {len(img.to_bmp())} bytes")
print(f"tga, rle: {len(img.to_tga(rle=True))} bytes")
# write_* encodes and writes the file; write() picks the format from the extension.
img.write_png("out/best.png", compression=9)
img.write_jpg("out/q40.jpg", quality=40)
img.write_bmp("out/picture.bmp")
img.write_tga("out/picture.tga", rle=True)
img.write("out/default.jpeg")
# An extension libstb does not write is an error, never a guess.
try:
img.write("out/picture.webp")
except ValueError:
print("webp: rejected")
# PNG is lossless: the pixels come back exactly. JPEG is not, and quality decides how far off they are.
back = stb.Image.decode(img.to_png())
print("png round trip exact:", "yes" if back.tobytes() == img.tobytes() else "no")
for quality in (20, 90):
jpeg = stb.Image.decode(img.to_jpg(quality=quality))
worst = max(abs(a - b) for a, b in zip(jpeg.tobytes(), img.tobytes()))
print(f"jpg quality {quality}: the worst channel value is off by {worst}")
png, compression 1: 226987 bytes
png, compression 9: 219947 bytes
jpg, quality 20: 7847 bytes
jpg, quality 60: 15760 bytes
jpg, quality 90: 35015 bytes
bmp: 406854 bytes
tga, rle: 403243 bytes
webp: rejected
png round trip exact: yes
jpg quality 20: the worst channel value is off by 87
jpg quality 90: the worst channel value is off by 44


The settings
| Format | Setting | Range | Default |
|---|---|---|---|
| PNG | compression | 1 to 9 | 8 |
| JPEG | quality | 1 to 100 | 90 |
| BMP | none | ||
| TGA | rle (run-length encoding) | true or false | true |
JPEG has no alpha channel, so the alpha of an RGBA picture is dropped when it is written as a JPEG. To put it on a background first, use flatten (channels and alpha).
Lossless and lossy
PNG, BMP and TGA keep every pixel exactly, so decoding what you encoded gives the same bytes back (the example checks it). JPEG does not: a lower quality makes a smaller file and moves each channel value further from the original. The last two lines of the output show the difference between quality 20 and quality 90.
Unknown extensions
write raises an error for an extension it does not write, and does not create the file. It is a ValueError.
API at a glance
| PNG bytes / file | img.to_png(compression=8), img.write_png(path, compression=8) |
|---|---|
| JPEG bytes / file | img.to_jpg(quality=90), img.write_jpg(path, quality=90) |
| BMP | img.to_bmp(), img.write_bmp(path) |
| TGA | img.to_tga(rle=True), img.write_tga(path, rle=True) |
| By extension | img.write(path) |
| The bytes are | bytes |