# PyGuetzli

[![Build Status](https://travis-ci.org/wanadev/pyguetzli.svg?branch=master)](https://travis-ci.org/wanadev/pyguetzli)
[![PYPI Version](https://img.shields.io/pypi/v/pyguetzli.svg)](https://pypi.python.org/pypi/pyguetzli)


**PyGuetzli** is a Python bindings for Google's [**Guetzli**][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.

[guetzli]: https://github.com/google/guetzli


## 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

```python
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

```python
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

```python
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

```python
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
