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

"UnboundLocalError: local variable 'values' referenced before assignment" when image.getexif().tobytes() on specific JPEGs #5555

Closed
christophebiocca opened this issue Jun 24, 2021 · 1 comment · Fixed by #5556
Labels
Bug Any unexpected behavior, until confirmed feature. Exif

Comments

@christophebiocca
Copy link

christophebiocca commented Jun 24, 2021

I can't post the picture itself because it's from a customer, but the offending exif data trying to be serialized is:

ifd: <PIL.TiffImagePlugin.ImageFileDirectory_v2 object at ...>
ifd.self._tags_v2.items : {34665: {}, 40962: 1920, 40963: 1440, 40961: 1}

That empty dictionary for 34665 seems to be the culprit.

The failing statement is the use of str(values) when is_ifd is true (and thus values never gets defined):

"<table: %d bytes>" % len(data) if len(data) >= 16 else str(values)

This will silently do the wrong thing instead of exploding if this is the second+ tag being serialized (as it will inherit values from the previous iteration of the loop.
It probably won't take that path for non-empty dictionaries, as those would have non-trivial data length.

A simple fix would be if len(data) >= 16 or is_ifd but I'm not super familiar with the codebase so it may not be the best choice.

@radarhere
Copy link
Member

Thanks for your analysis of the problem.

I've created #5556 to resolve this, with a simple test case. Rather than using or is_ifd, my suggestion is instead to set values when is_ifd is true as well.

@radarhere radarhere added Bug Any unexpected behavior, until confirmed feature. Exif labels Jun 25, 2021
@radarhere radarhere changed the title "UnboundLocalError: local variable 'values' referenced before assignment" when image.getexif().tobytes() on specific JPEGS "UnboundLocalError: local variable 'values' referenced before assignment" when image.getexif().tobytes() on specific JPEGs Jun 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature. Exif
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants