Skip to main content

Python bindings for Google's Guetzli, a JPEG encoder that optimises JPEG compression

Project description

Build Status PYPI Version

PyGuetzli is a Python bindings for Google’s **Guetzli**.

Description of Guetzli from official’s repo:

Guetzli is a JPEG encoder that aims for excellent compression density at high visual quality. Guetzli-generated images are typically 20-30% smaller than images of equivalent quality generated by libjpeg. Guetzli generates only sequential (nonprogressive) JPEGs due to faster decompression speeds they offer.

Building and Installing PyGuetzli

In order to build Guetzli, GCC, GNU Make and libpng are required. On Debian / Ubuntu, this can be installed with the following command:

sudo apt-get install build-essential libpng-dev

Installing from source

To build and install PyGuetzli from source, run the following command from the project’s root directory:

pip install .

Using PyGuetzli

Optimizing from a file

import pyguetzli

# Optimizing the image
image = pyguetzli.process_image_from_file("image.jpg", quality=95)

# Getting bytes of the optimized image
image_bytes = image.to_bytes()
print(image_bytes[:10])  # -> "\xFF\xD8\xFF\xE0\x00\x10JFIF"

# Writing the optimized image
image.save("optimized.jpg")

NOTE: Currently, only JPEG files are supported!

Optimizing from bytes

import pyguetzli

data = open("image.jpg", "rb").read()

# Optimizing the image
image = pyguetzli.process_image_from_bytes(data, quality=95)

# Getting bytes of the optimized image
image_bytes = image.to_bytes()
print(image_bytes[:10])  # -> "\xFF\xD8\xFF\xE0\x00\x10JFIF"

# Writing the optimized image
image.save("optimized.jpg")

NOTE: Currently, only JPEG files are supported!

Optimizing from RGB bytes

import pyguetzli

# This is a 2x2 px image with a red, a green, a blue and a white pixels
data = data = image_pixels = bytes(bytearray([
    0xFF, 0x00, 0x00,   0x00, 0xFF, 0x00,
    0x00, 0x00, 0xFF,   0xFF, 0xFF, 0xFF,
    ]))

# Generating an optimized JPEG image
image = pyguetzli.process_rgb_bytes(data, quality=95)

# Writing the optimized image
image.save("optimized.jpg")

Working with PIL / Pillow

import pyguetzli
from PIL import Image

# PIL image
image = Image.open("./test/image.png")

# Getting bytes from the PIL image
image_rgb_bytes = image.tobytes()

# Generating an optimized JPEG from the bytes
guetzli_image = pyguetzli.process_rgb_bytes(
        image_rgb_bytes,
        image.width,
        image.height,
        quality=95)

# Saving the image
guetzli_image.save("out.jpg")

Testing

pip install cffi pytest
python setup.py develop
pytest

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

pyguetzli-0.9.0.tar.gz (93.9 kB view hashes)

Uploaded Source

Supported by

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