A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/wanadev/mozjpeg-lossless-optimization below:

wanadev/mozjpeg-lossless-optimization: Python library to optimize JPEGs losslessly using MozJPEG

MozJPEG Lossless Optimization

This library optimizes JPEGs losslessly using MozJPEG.

To reduce the file sizes,

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.

Optimizing (losslessly) a JPEG:

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)

To preserve image metadata, you can set the copy parameter to mozjpeg_lossless_optimization.COPY_MARKERS.ALL:

output_jpeg_bytes = mozjpeg_lossless_optimization.optimize(
    input_jpeg_bytes,
    copy=mozjpeg_lossless_optimization.COPY_MARKERS.ALL,
)

Possible values for the copy parameter:

Converting an image to an optimized JPEG (using Pillow):

from io import BytesIO

from PIL import Image  # pip install pillow
import mozjpeg_lossless_optimization


def convert_to_optimized_jpeg(input_path, output_path):
    jpeg_io = BytesIO()

    with Image.open(input_path, "r") as image:
        image.convert("RGB").save(jpeg_io, format="JPEG", quality=90)

    jpeg_io.seek(0)
    jpeg_bytes = jpeg_io.read()

    optimized_jpeg_bytes = mozjpeg_lossless_optimization.optimize(jpeg_bytes)

    with open(output_path, "wb") as output_file:
        output_file.write(optimized_jpeg_bytes)


convert_to_optimized_jpeg("input.png", "optimized.jpg")

To install MozJPEG Lossless Optimization from PyPI, just run the following command:

pip install mozjpeg-lossless-optimization

Note

We provide precompiled packages for most common platforms. You may need to install additional build dependencies if there is no precompiled package available for your platform (see below).

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:

python3 setup.py install

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

Run tests:

pip install nox
pip -s test

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.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4