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

decode_base64_to_image: don't crash if EXIF data is broken #4764

Merged
merged 4 commits into from Jul 6, 2023

Conversation

akx
Copy link
Contributor

@akx akx commented Jul 3, 2023

Description

This adds a simple guard for decode_base64_to_image to not just throw when transposing the image fails.

I can't share the file that causes this issue, but the traceback is

Traceback (most recent call last):
  File "gradio/routes.py", line 439, in run_predict
    output = await app.get_blocks().process_api(
  File "gradio/blocks.py", line 1351, in process_api
    inputs = self.preprocess_data(fn_index, inputs, state)
  File "gradio/blocks.py", line 1201, in preprocess_data
    processed_input.append(block.preprocess(inputs[i]))
  File "gradio/components/image.py", line 247, in preprocess
    im = processing_utils.decode_base64_to_image(x)
  File "gradio/processing_utils.py", line 58, in decode_base64_to_image
    exif = img.getexif()
  File "python3.10/site-packages/PIL/PngImagePlugin.py", line 1028, in getexif
    return super().getexif()
  File "python3.10/site-packages/PIL/Image.py", line 1455, in getexif
    self._exif.load(exif_info)
  File "python3.10/site-packages/PIL/Image.py", line 3719, in load
    self._info = TiffImagePlugin.ImageFileDirectory_v2(self.head)
  File "python3.10/site-packages/PIL/TiffImagePlugin.py", line 507, in __init__
    raise SyntaxError(msg)
SyntaxError: not a TIFF file (header b"b'Exif\\x" not valid)

🎯 PRs Should Target Issues

This PR doesn't target an issue right now. If you like, I can open an issue that says the above.

@vercel
Copy link

vercel bot commented Jul 3, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
gradio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 6, 2023 3:25pm

@akx akx marked this pull request as ready for review July 3, 2023 07:25
@gradio-pr-bot
Copy link
Contributor

All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-4764-all-demos

@akx akx changed the title decode_base64_to_image: don't crash when if EXIF data is broken decode_base64_to_image: don't crash if EXIF data is broken Jul 3, 2023
@abidlabs
Copy link
Member

abidlabs commented Jul 3, 2023

Hi @akx we definitely need an example image or some other way to reproduce this, and a test to ensure that we don't have a regression in the future

@akx
Copy link
Contributor Author

akx commented Jul 3, 2023

@abidlabs Guess I'll close this then 👍

@akx akx closed this Jul 3, 2023
@akx akx reopened this Jul 4, 2023
@akx
Copy link
Contributor Author

akx commented Jul 4, 2023

@abidlabs Reopened, since I figured out a reliable repro 😁

@akx akx force-pushed the image-base64-fixes branch 2 times, most recently from a971679 to bf840d0 Compare July 4, 2023 14:00
@@ -55,10 +57,15 @@ def extract_base64_data(x: str) -> str:
def decode_base64_to_image(encoding: str) -> Image.Image:
image_encoded = extract_base64_data(encoding)
img = Image.open(BytesIO(base64.b64decode(image_encoded)))
exif = img.getexif()
# 274 is the code for image rotation and 1 means "correct orientation"
if exif.get(274, 1) != 1 and hasattr(ImageOps, "exif_transpose"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we remove this check exif.get(274, 1) != 1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abidlabs
Copy link
Member

abidlabs commented Jul 6, 2023

Thanks @akx tested and LGTM. Will merge in after CI runs

@abidlabs abidlabs merged commit 87e14e3 into gradio-app:main Jul 6, 2023
14 checks passed
@akx akx deleted the image-base64-fixes branch July 6, 2023 17:27
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 this pull request may close these issues.

None yet

3 participants