Skip to content
Snippets Groups Projects

Retry image download

Merged Yoann Schneider requested to merge retry-image-download into main
All threads resolved!
Files
3
@@ -50,10 +50,18 @@ def download_image(url):
Download an image and open it with Pillow
"""
assert url.startswith("http"), "Image URL must be HTTP(S)"
# Download the image
# Cannot use stream=True as urllib's responses do not support the seek(int) method,
# which is explicitly required by Image.open on file-like objects
resp = _retried_request(url)
try:
resp = _retried_request(url)
except requests.HTTPError as e:
if e.response.status_code >= 400:
# Retry with max instead of full as IIIF size
resp = _retried_request(url.replace("/full/", "/max/"))
else:
raise e
# Preprocess the image and prepare it for classification
image = Image.open(BytesIO(resp.content)).convert("RGB")
Loading