Guetzli Python Module

This module contains functions binded from the Guetzli C++ API.

pyguetzli.guetzli.DEFAULT_JPEG_QUALITY = 95

Default quality for outputted JPEGs

pyguetzli.guetzli.process_jpeg_bytes(bytes_in, quality=95)[source]

Generates an optimized JPEG from JPEG-encoded bytes.

Parameters:
  • bytes_in – the input image’s bytes

  • quality – the output JPEG quality (default 95)

Returns:

Optimized JPEG bytes

Return type:

bytes

Raises:

ValueError – Guetzli was not able to decode the image (the image is probably corrupted or is not a JPEG)

import pyguetzli

input_jpeg_bytes = open("./test/image.jpg", "rb").read()
optimized_jpeg = pyguetzli.process_jpeg_bytes(input_jpeg_bytes)
pyguetzli.guetzli.process_rgb_bytes(bytes_in, width, height, quality=95)[source]

Generates an optimized JPEG from RGB bytes.

Parameters:
  • bytes_in (bytes) – the input image’s bytes

  • width (int) – the width of the input image

  • height (int) – the height of the input image

  • quality (int) – the output JPEG quality (default 95)

Returns:

Optimized JPEG bytes

Return type:

bytes

Raises:

ValueError – the given width and height is not coherent with the bytes_in length.

import pyguetzli

# 2x2px RGB image
#                |    red    |   green   |
image_pixels  = b"\xFF\x00\x00\x00\xFF\x00"
image_pixels += b"\x00\x00\xFF\xFF\xFF\xFF"
#                |   blue    |   white   |

optimized_jpeg = pyguetzli.process_rgb_bytes(image_pixels, 2, 2)