Skip to main content

Optimize JPEGs losslessly using MozJPEG

Project description

Github Discord PYPI Version Build Status Black License

This library optimizes JPEGs losslessly using MozJPEG.

To reduce the file sizes,

  • the Huffman table of the JPEGs is optimized,

  • the baseline JPEGs are converted to progressive JPEGs,

  • and any metadata and ICC profiles are removed.

The JPEGs optimized with this library are identical to what you get using the jpegtran tool from MozJPEG with the -optimize, -progressive and -copy none options.

Usage

import mozjpeg_lossless_optimization

with open("./image.jpg", "rb") as input_jpeg_file:
    input_jpeg_bytes = input_jpeg_file.read()

output_jpeg_bytes = mozjpeg_lossless_optimization.optimize(input_jpeg_bytes)

with open("./out.jpg", "wb") as output_jpeg_file:
    output_jpeg_file.write(output_jpeg_bytes)

Install

From Sources

To install MozJPEG Lossless Optimization, MozJPEG will be compiled, so you will need a C compilator and cmake. On Debian / Ubuntu you can install everything you need with the following command:

sudo apt install build-essential cmake python3 python3-dev python3-pip python3-setuptools

Once everything installed, clone this repository:

git clone https://github.com/wanadev/mozjpeg-lossless-optimization.git

Then navigate to the project’s folder:

cd mozjpeg-lossless-optimization

Retrieve submodules:

git submodule init
git submodule update

And finally build and install using the following command (as root on Linux):

python3 setup.py install

Hacking

Get the source and build C lib and module:

# Install system dependencies
sudo apt install build-essential cmake python3 python3-dev python3-pip python3-setuptools

# Get the sources
git clone https://github.com/wanadev/mozjpeg-lossless-optimization.git
cd mozjpeg-lossless-optimization
git submodule init
git submodule update

# Create and activate a Python virtualenv
python3 -m venv __env__
source __env__/bin/activate

# Install Python dependencies in the virtualenv
pip install cffi

# Build MozJPEG
# This will generate files in ./mozjpeg/build/ folder
python setup.py build

# Build the CFFI module "in-place"
# This will create the ./mozjpeg_lossless_optimization/_mozjpeg_opti.*.so file on Linux
python ./mozjpeg_lossless_optimization/mozjpeg_opti_build.py

Lint:

pip install nox
nox -s lint

Licenses

MozJPEG Lossless Optimization is licensed under the BSD 3 Clause license. See the LICENSE file for more information.

MozJPEG is covered by three compatible BSD-style open source licenses. See its license file for more information.

Changelog

  • v0.9.0: First public release

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

mozjpeg-lossless-optimization-0.9.0.tar.gz (1.0 MB view hashes)

Uploaded Source

Built Distributions

mozjpeg_lossless_optimization-0.9.0-cp39-cp39-win_amd64.whl (58.3 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

mozjpeg_lossless_optimization-0.9.0-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (125.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

mozjpeg_lossless_optimization-0.9.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl (140.9 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

mozjpeg_lossless_optimization-0.9.0-cp39-cp39-macosx_10_14_x86_64.whl (74.1 kB view hashes)

Uploaded CPython 3.9 macOS 10.14+ x86-64

mozjpeg_lossless_optimization-0.9.0-cp38-cp38-win_amd64.whl (58.3 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

mozjpeg_lossless_optimization-0.9.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (126.3 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

mozjpeg_lossless_optimization-0.9.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl (141.2 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

mozjpeg_lossless_optimization-0.9.0-cp38-cp38-macosx_10_14_x86_64.whl (74.1 kB view hashes)

Uploaded CPython 3.8 macOS 10.14+ x86-64

mozjpeg_lossless_optimization-0.9.0-cp37-cp37m-win_amd64.whl (58.3 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

mozjpeg_lossless_optimization-0.9.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (125.8 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

mozjpeg_lossless_optimization-0.9.0-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl (140.8 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

mozjpeg_lossless_optimization-0.9.0-cp37-cp37m-macosx_10_14_x86_64.whl (74.1 kB view hashes)

Uploaded CPython 3.7m macOS 10.14+ x86-64

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