raise exception on invalid images (#999)

# What does this PR do?
This PR is meant to handle cases in which the images provided are
invalid.

## Who can review?

Anyone in the community is free to review the PR once the tests have
passed. Feel free to tag
members/contributors who may be interested in your PR.

@Narsil

---------

Co-authored-by: Nicolas Patry <patry.nicolas@protonmail.com>
This commit is contained in:
Leo Tronchon 2023-10-03 10:26:10 +02:00 committed by GitHub
parent bd998d8797
commit b8fefa6b55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 2 deletions

View File

@ -194,9 +194,14 @@ class IdeficsImageProcessor(BaseImageProcessor):
if isinstance(image_url_or_urls, list):
return [self.fetch_images(x) for x in image_url_or_urls]
elif isinstance(image_url_or_urls, str):
response = requests.get(image_url_or_urls, stream=True, headers=headers)
response = requests.get(image_url_or_urls, stream=True, headers=headers, timeout=(1, 5))
response.raise_for_status()
return Image.open(BytesIO(response.content))
try:
image = Image.open(BytesIO(response.content))
image.verify()
except Exception:
raise ValueError(f"Could not load image from url {image_url_or_urls}")
return image
else:
raise ValueError(
f"only a single or a list of entries is supported but got type={type(image_url_or_urls)}"