pyoxipng
raw JSON → 9.1.1 verified Sat May 09 auth: no python
Python wrapper for oxipng, a multithreaded PNG optimizer. Version 9.1.1 supports Python >=3.8 and is actively maintained. Releases follow oxipng upstream closely, typically every few months.
pip install pyoxipng Common errors
error TypeError: expected bytes, got str ↓
cause Passing a file path string instead of bytes to optimize_png.
fix
Open the file in binary mode:
with open('file.png', 'rb') as f: data = f.read(). error AttributeError: module 'pyoxipng' has no attribute 'optimize_png' ↓
cause Old version (pre-7.0) had different API or the module is not installed correctly.
fix
Upgrade to 9.1.1:
pip install --upgrade pyoxipng and use from pyoxipng import optimize_png. error NameError: name 'optimize_png' is not defined ↓
cause Forgot to import the function correctly.
fix
Add
from pyoxipng import optimize_png at the top of the script. Warnings
gotcha optimize_png accepts bytes, not a file path. Passing a file path will fail silently or raise TypeError. ↓
fix Open the file in binary read mode and pass bytes.
breaking Version 7.0.0 changed the return type from (bytes, bool) to bytes. The second element (whether the image was changed) was removed. ↓
fix Update code from `result, changed = optimize_png(data)` to `result = optimize_png(data)`. Use `if result != data:` to check if optimized.
deprecated The `level` parameter in optimize_png is deprecated since 8.0.0, use `opts` dict instead. ↓
fix Replace `optimize_png(data, level=6)` with `optimize_png(data, opts=dict(level=6))`.
Imports
- optimize_png wrong
import oxipngcorrectfrom pyoxipng import optimize_png
Quickstart
from pyoxipng import optimize_png
with open('input.png', 'rb') as f:
data = f.read()
result = optimize_png(data)
with open('output.png', 'wb') as f:
f.write(result)