A Python implementation of the QOI (Quite OK Image) format
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyqoi_lib-0.1.0.tar.gz.
File metadata
- Download URL: pyqoi_lib-0.1.0.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73124f1ec7a5a165666adba01188db0abe3648bf13dd953af527262981d80b10
|
|
| MD5 |
694d08820a24a28d22403b26b9ea221c
|
|
| BLAKE2b-256 |
b28c8f74403eb5d1492c50562445784c4788cdd59f52d2fb6bc1e070a4f0a585
|
File details
Details for the file pyqoi_lib-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pyqoi_lib-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60733117609279bcc46b59ab6c81444c5b38f7a3f887ea6f33139831d89d55ae
|
|
| MD5 |
eceb94ed90b12da55e942bd86480a129
|
|
| BLAKE2b-256 |
701047d44882a2eb8ebb8899b4f8bf8dbc34ec028376785e398aec4448f8183a
|