Skip to main content

No project description provided

Project description

pyoxipng

CI

Python wrapper for multithreaded .png image file optimizer oxipng (written in Rust). Use pyoxipng to reduce the file size of your PNG images.

Installation

Install from PyPI:

pip install pyoxipng

Import in your Python code:

import oxipng

Usage

To optimize an existing file on disk:

oxipng.optimize("/path/to/image.png", "/path/to/image-optimized.png")

Second argument is optional; if not provided, will overwrite the existing file.

To optimize a file already-loaded in Python as a bytes object:

data = ...  # bytes of png data

optimized_data = oxipng.optimize_from_memory(data)

# Save the result
with open("/path/to/image-optimized.png", "wb") as f:
    f.write(optimized_data)

Both functions raise oxipng.PngError if the optimization cannot be completed.

Options

Both the optimize and optimize_from_memory functions take the following options as keyword arguments.

Example usage:

oxipng.optimize("/path/to/image.png", level=6, backup=True, interlace=1)
Option Description Type Default
level Set the optimization level to an integer between 0 and 6 (inclusive) int 2
backup Whether the input file should be backed up before writing the output bool False
fix_errors Attempt to fix errors when decoding the input file rather than throwing PngError bool False
pretend Don't actually write any output file, just calculate the best results bool False
force Write to output even if there was no improvement in compression bool False
preserve_attrs Ensure the output file has the same permissions as the input file bool False
filter Which filters to try on the file [0-5] set[int] {0,5}
interlace Whether to change the interlacing type of the file. 0 means disable interlacing. 1 means enable it. None means leave as is. int | None None
bit_depth_reduction Whether to attempt bit depth reduction bool True
color_type_reduction Whether to attempt color type reduction bool True
palette_reduction Whether to attempt palette reduction bool True
grayscale_reduction Whether to attempt grayscale reduction bool True
idat_recoding If any type of reduction is performed, IDAT recoding will be performed regardless of this setting bool True
use_heuristics Whether to use heuristics to pick the best filter and compression. Intended for use with level=1 bool False
timeout Maximum amount of time to spend (in milliseconds) on optimizations. Further potential optimizations are skipped if the timeout is exceeded. int | None None

Development

  1. Install Rust
  2. Install Python 3.7+
  3. Install Pipenv
  4. Clone this repository and navigate to it via command line
    git clone https://github.com/nfrasser/pyoxipng.git
    cd pyoxipng
    
  5. Install dependencies
    pipenv install --dev
    
  6. Activate the dev environment
    pipenv shell
    
  7. Build
    maturin develop
    
  8. Run tests
    pytest
    
  9. Format code
    black .
    

License

MIT

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

pyoxipng-0.1.0.tar.gz (293.9 kB view hashes)

Uploaded Source

Built Distributions

pyoxipng-0.1.0-pp37-pypy37_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.4 MB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ x86-64

pyoxipng-0.1.0-cp310-none-win_amd64.whl (453.5 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

pyoxipng-0.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.5+ x86-64

pyoxipng-0.1.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.1 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

pyoxipng-0.1.0-cp39-none-win_amd64.whl (453.5 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

pyoxipng-0.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.4 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.5+ x86-64

pyoxipng-0.1.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.1 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

pyoxipng-0.1.0-cp38-none-win_amd64.whl (453.6 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

pyoxipng-0.1.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.4 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

pyoxipng-0.1.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.1 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

pyoxipng-0.1.0-cp37-none-win_amd64.whl (453.5 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

pyoxipng-0.1.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.4 MB view hashes)

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

pyoxipng-0.1.0-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.1 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

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