Skip to main content

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 file
  • desc: A QoiHeader object that will be populated with image information
  • channels: 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 file
  • data: Raw pixel data as bytes
  • desc: A QoiHeader object with image information
  • out_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 bytes
  • desc: A QoiHeader object with image information
  • out_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 bytes
  • size: Size of the encoded data
  • desc: A QoiHeader object that will be populated with image information
  • channels: 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyqoi_lib-0.1.0.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyqoi_lib-0.1.0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

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

Hashes for pyqoi_lib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 73124f1ec7a5a165666adba01188db0abe3648bf13dd953af527262981d80b10
MD5 694d08820a24a28d22403b26b9ea221c
BLAKE2b-256 b28c8f74403eb5d1492c50562445784c4788cdd59f52d2fb6bc1e070a4f0a585

See more details on using hashes here.

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

Hashes for pyqoi_lib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 60733117609279bcc46b59ab6c81444c5b38f7a3f887ea6f33139831d89d55ae
MD5 eceb94ed90b12da55e942bd86480a129
BLAKE2b-256 701047d44882a2eb8ebb8899b4f8bf8dbc34ec028376785e398aec4448f8183a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page