Skip to content

Image Utils

Image utilities for preparing image bytes for Pillow.

prepare_image_bytes_for_pil(image_bytes)

Prepare image bytes so Pillow can open them.

Tries to open the image with Pillow. On UnidentifiedImageError for PNG files, strips zTXt chunks and retries. Valid zTXt is preserved when Pillow can open the original bytes.

Parameters:

Name Type Description Default
image_bytes bytes

Raw image file bytes.

required

Returns:

Name Type Description
bytes bytes

Original bytes if Pillow can open them, or PNG bytes with zTXt removed.

Raises:

Type Description
UnidentifiedImageError

If Pillow cannot open the image even after stripping zTXt.