Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing quality setting leads to error with 1.4.0 #32

Closed
boidolr opened this issue Oct 12, 2023 · 1 comment · Fixed by #33
Closed

Missing quality setting leads to error with 1.4.0 #32

boidolr opened this issue Oct 12, 2023 · 1 comment · Fixed by #33

Comments

@boidolr
Copy link

boidolr commented Oct 12, 2023

Minimal example:

import pillow_avif
import PIL
import io

# just some encoded avif
image = b'\x00\x00\x00 ftypavif\x00\x00\x00\x00avifmif1miafMA1B\x00\x00\x01\x8dmeta\x00\x00\x00\x00\x00\x00\x00(hdlr\x00\x00\x00\x00\x00\x00\x00\x00pict\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00libavif\x00\x00\x00\x00\x0epitm\x00\x00\x00\x00\x00\x01\x00\x00\x00,iloc\x00\x00\x00\x00D\x00\x00\x02\x00\x01\x00\x00\x00\x01\x00\x00\x01\xc9\x00\x00\x00\x16\x00\x02\x00\x00\x00\x01\x00\x00\x01\xb5\x00\x00\x00\x14\x00\x00\x00Biinf\x00\x00\x00\x00\x00\x02\x00\x00\x00\x1ainfe\x02\x00\x00\x00\x00\x01\x00\x00av01Color\x00\x00\x00\x00\x1ainfe\x02\x00\x00\x00\x00\x02\x00\x00av01Alpha\x00\x00\x00\x00\x1airef\x00\x00\x00\x00\x00\x00\x00\x0eauxl\x00\x02\x00\x01\x00\x01\x00\x00\x00\xc3iprp\x00\x00\x00\x9dipco\x00\x00\x00\x14ispe\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x05\x00\x00\x00\x10pixi\x00\x00\x00\x00\x03\x08\x08\x08\x00\x00\x00\x0cav1C\x81\x00\x0c\x00\x00\x00\x00\x13colrnclx\x00\x02\x00\x02\x00\x06\x80\x00\x00\x00\x0epixi\x00\x00\x00\x00\x01\x08\x00\x00\x00\x0cav1C\x81\x00\x1c\x00\x00\x00\x008auxC\x00\x00\x00\x00urn:mpeg:mpegB:cicp:systems:auxiliary:alpha\x00\x00\x00\x00\x1eipma\x00\x00\x00\x00\x00\x00\x00\x02\x00\x01\x04\x01\x02\x83\x04\x00\x02\x04\x01\x05\x86\x07\x00\x00\x002mdat\x12\x00\n\x05\x18\x08\xa4\xe5P2\t\x1f\xf0\x00\x01\x00\x14d\xd4\x90\x12\x00\n\x08\x18\x08\xa4\xe4\x81\x01\x03B2\x08\x1f\xf0\x00\x00@\x00\x11j'

im = PIL.Image.open(io.BytesIO(image))
im.save("test.avif", format="avif", qmin=10, qmax=50)

# error: ... (cannot access local variable 'quality' where it is not associated with a value)

I think the issue is that quality is only set inside of the if here:

if qmin is None and qmax is None:
# The min and max quantizer settings in libavif range from 0 (best quality)
# to 63 (worst quality). If neither are explicitly specified, we use a 0-100
# quality scale (default 75) and calculate the qmin and qmax from that.
#
# - qmin is 0 for quality >= 64. Below that, qmin has an inverse linear
# relation to quality (i.e., quality 63 = qmin 1, quality 0 => qmin 63)
# - qmax is 0 for quality=100, then qmax increases linearly relative to
# quality decreasing, until it flattens out at quality=37.
quality = info.get("quality", 75)
if not isinstance(quality, int) or quality < 0 or quality > 100:
raise ValueError("Invalid quality setting")
qmin = max(0, min(64 - quality, 63))
qmax = max(0, min(100 - quality, 63))

fdintino added a commit that referenced this issue Oct 12, 2023
Moves the qmin/qmax conversion logic from AvifImagePlugin to
_avif.c, where we know whether avif supports the quality encoder option.

This allows users to continue to pass qmin/qmax to save(), even if they
are using libavif >= 1.0.0

fixes #32
fdintino added a commit that referenced this issue Oct 12, 2023
* fix: cannot access local variable 'quality' ...

Moves the qmin/qmax conversion logic from AvifImagePlugin to
_avif.c, where we know whether avif supports the quality encoder option.

This allows users to continue to pass qmin/qmax to save(), even if they
are using libavif >= 1.0.0

fixes #32

* fix qminmax quality test
@boidolr
Copy link
Author

boidolr commented Oct 13, 2023

Thank you @fdintino !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant