oxipng.PngError – optimization could not be completed
Examples:
data = ... # bytes of png data
optimized_data = oxipng.optimize_from_memory(data)
with open("/path/to/image-optimized.png", "wb") as f:
f.write(optimized_data)
oxipng.RawImage
Create an optimized PNG file from raw image data:
raw = oxipng.RawImage(data, width, height)
optimized_data = raw.create_optimized_png()
By default, assumes the input data is 8-bit, row-major RGBA, where every 4 bytes represents one pixel with Red-Green-Blue-Alpha channels. To interpret non-RGBA data, specify a color_type parameter with the oxipng.ColorType class:
Method
Description
oxipng.ColorType.grayscale(int | None)
Grayscale, with one color channel. Specify optional shade of gray that should be rendered as transparent.
oxipng.ColorType.rgb(tuple[int, int, int])
RGB, with three color channels. Specify optional color value that should be rendered as transparent.
Indexed, with one byte per pixel representing a color from the palette. Specify palette containing the colors used, up to 256 entries.
oxipng.ColorType.grayscale_alpha()
Grayscale + Alpha, with two color channels.
oxipng.ColorType.rgba()
RGBA, with four color channels.
Parameters:
data(bytes | bytearray) – Raw image data bytes. Format depends on color_type and bit_depth parameters
width(int) – Width of raw image, in pixels
height(int) – Height of raw image, in pixels
color_type([oxipng.ColorType, optional) – Descriptor for color type used to represent this image. Optional, defaults to oxipng.ColorType.rgba()
bit_depth(int, optional) – Bit depth of raw image. Optional, defaults to 8
Examples:
Save RGB image data from a JPEG file, interpreting black pixels as transparent.
from PIL import Image
import numpy as np
# Load an image file with Pillow
jpg = Image.open("/path/to/image.jpg")
# Convert to RGB numpy array
rgb_array = np.array(jpg.convert("RGB"), dtype=np.uint8)
height, width, channels = rgb_array.shape
# Create raw image with sRGB color profile
data = rgb_array.tobytes()
color_type = oxipng.ColorType.rgb((0, 0, 0)) # black is transparent
raw = oxipng.RawImage(data, width, height, color_type=color_type)
raw.add_png_chunk(b"sRGB", b"\0")
# Optimize and save
optimized = raw.create_optimized_png(level=6)
with open("/path/to/image/optimized.png", "wb") as f:
f.write(optimized)
Save with data where bytes reference a color palette
data = b"\0\1\2..." # get index data
palette = [[0, 0, 0, 255], [1, 23, 234, 255], ...]
color_type = oxipng.ColorType.indexed(palette)
raw = oxipng.RawImage(data, 100, 100, color_type=color_type)
optimized = raw.create_optimized_png()
Methods:
add_png_chunk(name, data)
Add a png chunk, such as b"iTXt", to be included in the output
Parameters:
name(bytes) – PNG chunk identifier
data(bytes | bytarray)
Returns:
None
add_icc_profile(data)
Add an ICC profile for the image
Parameters:
data(bytes) – ICC profile data
Returns:
None
create_optimized_png(**kwargs)
Create an optimized png from the raw image data using the options provided