PyQOI
A Python implementation of the QOI (Quite OK Image) format.
About QOI
QOI (Quite OK Image format) is a fast, lossless image compression format. Its compression ratios are similar to PNG, but it offers significantly faster encoding and decoding. For more information about the QOI format, visit the official QOI website.
Installation
pip install pyqoi
Usage
Reading a QOI image
from pyqoi import QoiHeader, read
import numpy as np
from PIL import Image
# Create a header object
header = QoiHeader(width=0, height=0, channels=0, colorspace=0)
# Read pixels from a QOI file
pixels = read("image.qoi", header)
if pixels is not None:
# Convert to numpy array for further processing or displaying with PIL
pixels_array = np.frombuffer(pixels, dtype=np.uint8)
pixels_array = pixels_array.reshape((header.height, header.width, header.channels))
# Create PIL image
if header.channels == 3:
img = Image.fromarray(pixels_array, mode="RGB")
else:
img = Image.fromarray(pixels_array, mode="RGBA")
img.show()
Writing a QOI image
from pyqoi import QoiHeader, write
from PIL import Image
import numpy as np
# Load an image with PIL
pil_img = Image.open("image.png")
# Convert to RGB or RGBA
if pil_img.mode != "RGB" and pil_img.mode != "RGBA":
pil_img = pil_img.convert("RGBA")
# Get image data as numpy array
img_array = np.array(pil_img)
channels = 4 if pil_img.mode == "RGBA" else 3
# Create QOI header
header = QoiHeader(
width=pil_img.width,
height=pil_img.height,
channels=channels,
colorspace=0 # 0 for sRGB with linear alpha
)
# Get bytes from numpy array
pixels = img_array.tobytes()
# Write to QOI file
write("output.qoi", pixels, header, len(pixels))
API Reference
Classes
QoiHeader
@dataclass
class QoiHeader:
width: np.uint32 # image width
height: np.uint32 # image height
channels: np.uint8 # 3 if RGB, 4 if RGBA
colorspace: np.uint8 # 0 = sRGB with linear alpha, 1 = all channels linear
Functions
read(filename, desc, channels=0)
Reads a QOI image file and decodes it to raw pixel data.
filename: Path to the QOI filedesc: AQoiHeaderobject that will be populated with image informationchannels: Optional. Number of channels to use (0 to use the file's native channels)- Returns: A bytes object containing the raw pixel data
write(filename, data, desc, out_len)
Encodes raw pixel data and writes it to a QOI file.
filename: Output path for the QOI filedata: Raw pixel data as bytesdesc: AQoiHeaderobject with image informationout_len: Length of the pixel data in bytes
encode(data, desc, out_len)
Encodes raw pixel data to QOI format.
data: Raw pixel data as bytesdesc: AQoiHeaderobject with image informationout_len: Length of the pixel data in bytes- Returns: A tuple of (encoded_data, encoded_length)
decode(data, size, desc, channels=0)
Decodes QOI format data to raw pixels.
data: QOI encoded data as bytessize: Size of the encoded datadesc: AQoiHeaderobject that will be populated with image informationchannels: Optional. Number of channels to use (0 to use the file's native channels)- Returns: A bytes object containing the raw pixel data
License
MIT
Release files for pyqoi-lib 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyqoi_lib-0.1.0.tar.gz | 9.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyqoi_lib-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.6 kB
Release files / pyqoi_lib-0.1.0.tar.gz
| Download URL | pyqoi_lib-0.1.0.tar.gz |
|---|---|
| Size | 9.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73124f1ec7a5a165666adba01188db0abe3648bf13dd953af527262981d80b10
|
|
BLAKE2b-256 checksum How to use checksums |
b28c8f74403eb5d1492c50562445784c4788cdd59f52d2fb6bc1e070a4f0a585
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.1
|
Release files / pyqoi_lib-0.1.0-py3-none-any.whl
| Download URL | pyqoi_lib-0.1.0-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
60733117609279bcc46b59ab6c81444c5b38f7a3f887ea6f33139831d89d55ae
|
|
BLAKE2b-256 checksum How to use checksums |
701047d44882a2eb8ebb8899b4f8bf8dbc34ec028376785e398aec4448f8183a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.1
|