Skip to main content

Python wrapper for multithreaded .png image file optimizer oxipng

Project description

pyoxipng

CI PyPI

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

Jump to a section

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 accept 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
alphas Alpha filtering strategies to use. Use enum values from oxipng.AlphaOptim set[AlphaOptim] {AlphaOptim.NoOp}
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
strip Which headers to strip from the PNG file, if any. Specify with oxipng.Headers Headers Headers.none()
deflate Which DEFLATE algorithm to use. Specify an instance of oxipng.Zlib, oxipng.Zopfli or oxipng.Libdeflater Zlib | Zopfli | Libdeflater Zlib()
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

alphas

Initialize the alphas set with any of the following enum options:

  • oxipng.AlphaOptim.NoOp
  • oxipng.AlphaOptim.Black
  • oxipng.AlphaOptim.White
  • oxipng.AlphaOptim.Up
  • oxipng.AlphaOptim.Right
  • oxipng.AlphaOptim.Down
  • oxipng.AlphaOptim.Left

strip

Initialize the strip option by calling one of the following static methods provided by the oxipng.Headers class.

Method Description
oxipng.Headers.none() None
oxipng.Headers.strip(list[str]) Remove specific chunks
oxipng.Headers.safe() Headers that won't affect rendering (all but cHRM, gAMA, iCCP, sBIT, sRGB, bKGD, hIST, pHYs, sPLT)
oxipng.Headers.keep(set[str]) Remove all non-critical chunks except these
oxipng.Headers.all() All non-critical headers

deflate

Initialize the deflate option by instantiating one of the following classes:

  • Zlib(compression: list[int] = [9], strategies: list[int] = [0, 1, 2, 3], window: int = 15)
    • compression: Which zlib compression levels to try on the file (1-9)
    • strategies: Which zlib compression strategies to try on the file (0-3)
    • window: Window size to use when compressing the file, as 2^window bytes (8-15). Doesn't affect compression but may affect speed and memory usage
  • Zopfli(iterations: int)
    • iterations: The number of compression iterations to do.
  • Libdeflater()

Examples:

oxipng.optimize("in.png", deflate=Zlib(compression=[7, 8], window=8))
oxipng.optimize("in.png", deflate=Zopfli(3))
oxipng.optimize("in.png", deflate=Libdeflater())

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-6.0.0.tar.gz (299.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyoxipng-6.0.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

pyoxipng-6.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

pyoxipng-6.0.0-cp310-none-win_amd64.whl (525.8 kB view details)

Uploaded CPython 3.10Windows x86-64

pyoxipng-6.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

pyoxipng-6.0.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.3 MB view details)

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

pyoxipng-6.0.0-cp39-none-win_amd64.whl (526.1 kB view details)

Uploaded CPython 3.9Windows x86-64

pyoxipng-6.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

pyoxipng-6.0.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.3 MB view details)

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

pyoxipng-6.0.0-cp38-none-win_amd64.whl (526.2 kB view details)

Uploaded CPython 3.8Windows x86-64

pyoxipng-6.0.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

pyoxipng-6.0.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.3 MB view details)

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

pyoxipng-6.0.0-cp37-none-win_amd64.whl (526.2 kB view details)

Uploaded CPython 3.7Windows x86-64

pyoxipng-6.0.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

pyoxipng-6.0.0-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.3 MB view details)

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

File details

Details for the file pyoxipng-6.0.0.tar.gz.

File metadata

  • Download URL: pyoxipng-6.0.0.tar.gz
  • Upload date:
  • Size: 299.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for pyoxipng-6.0.0.tar.gz
Algorithm Hash digest
SHA256 776a822dfccc850c96459bbdb47057758e671f80fbb5c2678f4cbb364a085f59
MD5 915de5981948fa997ae183dff4c13f80
BLAKE2b-256 0beac53a1713a8b020581215e0ad7a2402f443561aa6866411a7a60acf5fd48f

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8ede4273277bb4eca59722abb4c5984b0a000b29ed47020720611476e919ef29
MD5 8286f208702147e6fd097b6ced382c9a
BLAKE2b-256 45cf1eeafc44f80c5c4dcecf5e96ab88ea9f93f693689e06b1141094e9fb107a

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 093eb20b7f6669b3ea70211dfba45e594059d881a921f0b420e345a17719e412
MD5 2f903abf456a7c432cd64a7abb121455
BLAKE2b-256 2d0daa9798fcfc87848768e73d0c4186cc38f0eaf5ba5e496c2a41b00d149e55

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp310-none-win_amd64.whl.

File metadata

  • Download URL: pyoxipng-6.0.0-cp310-none-win_amd64.whl
  • Upload date:
  • Size: 525.8 kB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for pyoxipng-6.0.0-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 cd9a9f89df502b01769c75fe557370d871318af80d8ae474b9d3f3212d4b0920
MD5 45fd1890a84a15e9b35acb65bb07f451
BLAKE2b-256 141d42c3b688f102888fcf6e25dda2bcc8605d5ffcb6241832cc97c585824228

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6f6e931612e8b846d09a897f41d5bbf504d20f010b05fc65f8993bc846ea0037
MD5 4e078c44d939880ef57f64e284d582f7
BLAKE2b-256 30e1ff1ed44168178878903fe299486dddab367caf95e7e46fca4be269ec26f5

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 4ed78425dbd6d97bcd7dc25ba3e7739efe2025844b8f8a1ad9af01861739daf8
MD5 41a77400d80d4b0b13021810c96b49b8
BLAKE2b-256 9b09f78477d0dfee34e54218e1d9e4359a0ee8bd34dce4eaaf9abd8ebed60be0

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp39-none-win_amd64.whl.

File metadata

  • Download URL: pyoxipng-6.0.0-cp39-none-win_amd64.whl
  • Upload date:
  • Size: 526.1 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for pyoxipng-6.0.0-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 e2e35257f3321f42730bd707c7198876352fa3be4e81d4608f0d84922f1fa05b
MD5 0e2d11725d961248a19f8f0f3f65dace
BLAKE2b-256 d10da76c9c68d00ee2aacd43bf092cd9c020629330496189cb96370a8b5dd2bf

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 639fe5b7ee0c92005d684bf44070214dbcb13788cc3c3149e5da1f413c56c36f
MD5 ccfbec2a5ce560b3bd58827ff1efa06a
BLAKE2b-256 6a56c300287ad2ee79c4c32efddcf2381e048e2e01a3fa8d5a6e3a84ffcd920c

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 78a39dd7d230f354ee4760efb86d931b5559033b9f91f7f22b5828d79c748b1f
MD5 f988d8c83a995243aed21080f34f420e
BLAKE2b-256 ef90e7ca551281c40fe31c65e8e4eada71925b1e72efb08c060d4b8067fea7f8

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp38-none-win_amd64.whl.

File metadata

  • Download URL: pyoxipng-6.0.0-cp38-none-win_amd64.whl
  • Upload date:
  • Size: 526.2 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for pyoxipng-6.0.0-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 8ac320e18e32270e6f6b1057645b826cd61c0cc60d6bb0dab51769fc2fa21b8a
MD5 a14aa69567cff3daa5f640008c2b42df
BLAKE2b-256 5d8980e483afd12eca8ee6ff2c703392d80189b9a5a084e3be51534014c09681

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7c04bc60dbad397be8dbc6d7bd6b25c151f3bec7d03fa1f2b453a61a33bb6700
MD5 70d623695bd61499951b20423a2194e8
BLAKE2b-256 bf6afcea7db8e64b6c480e652bb910351f19579339b600777d40f391ea54a70d

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 15c23d5e628b71c6429937b7de58476b947529b5138678993033c175b9452dea
MD5 f5aaf26795e4b2f5269455eed45a799a
BLAKE2b-256 41313938928fa9786a363c9fa6ce282dca08a9d74a7cc27b07557d4e33e4ad0e

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp37-none-win_amd64.whl.

File metadata

  • Download URL: pyoxipng-6.0.0-cp37-none-win_amd64.whl
  • Upload date:
  • Size: 526.2 kB
  • Tags: CPython 3.7, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.7

File hashes

Hashes for pyoxipng-6.0.0-cp37-none-win_amd64.whl
Algorithm Hash digest
SHA256 69795a244c581aa97d950e4cf62f0f6dc2ef3212139a1cc2b934f28ffb26bb0b
MD5 ffb98b9250cb6822cba40c890d5c41a8
BLAKE2b-256 60b5184f96bc406242532401d415f1e0fc20768482f3ca844440e79fe4cb7f7d

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4e2bc69fcc3a34487750a436fc3e53d7c41e9c41eaf51f81965cd488be075dab
MD5 4024fcf635b58566634bd26b6e86a1e9
BLAKE2b-256 8d77d062efd103852df935e81990f4e45d6250e63461c095bba5c652ab76aa03

See more details on using hashes here.

File details

Details for the file pyoxipng-6.0.0-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for pyoxipng-6.0.0-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 04d13684fb5d89d5314f6b597693dcb1ce0bbcc208c34b2678781fcdd008d102
MD5 c2ec9becec42817ccca8686c484197ce
BLAKE2b-256 0e3623579d1f0f4df29ee3f111cd686b51f1558b70eb0ec27531980a39c9c3c2

See more details on using hashes here.

Supported by

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