Skip to content
Snippets Groups Projects
Commit 76908023 authored by Bastien Abadie's avatar Bastien Abadie
Browse files

Do not try to load remote images from file system

parent 7a9e5d81
No related branches found
No related tags found
1 merge request!45Do not try to load remote images from file system
Pipeline #78093 passed
# -*- coding: utf-8 -*-
import os
from collections import namedtuple
from io import BytesIO
from math import ceil
......@@ -24,10 +25,17 @@ def open_image(path, mode="RGB"):
"""
Open an image from a path or a URL
"""
try:
image = Image.open(path)
except IOError:
if (
path.startswith("http://")
or path.startswith("https://")
or not os.path.exists(path)
):
image = download_image(path)
else:
try:
image = Image.open(path)
except (IOError, ValueError):
image = download_image(path)
if image.mode != mode:
image = image.convert(mode)
......
......@@ -6,7 +6,7 @@ from pathlib import Path
import pytest
from PIL import Image, ImageChops, ImageOps
from arkindex_worker.image import download_tiles
from arkindex_worker.image import download_tiles, open_image
FIXTURES = Path(__file__).absolute().parent / "data"
TILE = FIXTURES / "test_image.jpg"
......@@ -101,3 +101,29 @@ def test_download_tiles_small(responses):
with pytest.raises(ValueError) as e:
download_tiles("http://nowhere")
assert str(e.value) == "Expected size 181×240 for tile 0,0, but got 1×1"
@pytest.mark.parametrize(
"path, is_local",
(
("http://somewhere/test.jpg", False),
("https://somewhere/test.jpg", False),
("path/to/something", True),
("/absolute/path/to/something", True),
),
)
def test_open_image(path, is_local, monkeypatch):
"""Check if the path triggers a local load or a remote one"""
monkeypatch.setattr("os.path.exists", lambda x: True)
monkeypatch.setattr(Image, "open", lambda x: Image.new("RGB", (1, 10)))
monkeypatch.setattr(
"arkindex_worker.image.download_image", lambda x: Image.new("RGB", (10, 1))
)
image = open_image(path)
if is_local:
assert image.size == (1, 10)
else:
assert image.size == (10, 1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment