Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
import math
from io import BytesIO
from pathlib import Path
import pytest
from PIL import Image, ImageChops, ImageOps
from arkindex_worker.image import download_tiles
FIXTURES = Path(__file__).absolute().parent / "data"
TILE = FIXTURES / "test_image.jpg"
FULL_IMAGE = FIXTURES / "tiled_image.jpg"
def _root_mean_square(img_a, img_b):
"""
Get the root-mean-square difference between two images for fuzzy matching
See https://effbot.org/zone/pil-comparing-images.htm
"""
h = ImageChops.difference(img_a, img_b).histogram()
return math.sqrt(
sum((value * ((idx % 256) ** 2) for idx, value in enumerate(h)))
/ float(img_a.size[0] * img_a.size[1])
)
def test_download_tiles(responses):
expected = Image.open(FULL_IMAGE).convert("RGB")
with TILE.open("rb") as tile:
tile_bytes = tile.read()
responses.add(
responses.GET,
"http://nowhere/info.json",
json={"width": 543, "height": 720, "tiles": [{"width": 181, "height": 240}]},
)
for x in (0, 181, 362):
for y in (0, 240, 480):
responses.add(
responses.GET,
f"http://nowhere/{x},{y},181,240/full/0/default.jpg",
body=tile_bytes,
)
actual = download_tiles("http://nowhere")
assert _root_mean_square(expected, actual) <= 5.0
def test_download_tiles_crop(responses):
"""
Ensure download_tiles does not care about tiles that are slightly bigger than expected
(occasional issue with the Harvard IDS image server where 1024×1024 tiles sometimes are returned as 1024x1025)
"""
expected = Image.open(FULL_IMAGE).convert("RGB")
tile_bytes = BytesIO()
with TILE.open("rb") as tile:
# Add one extra pixel to each tile to return slightly bigger tiles
ImageOps.pad(Image.open(tile), (181, 241)).save(tile_bytes, format="jpeg")
tile_bytes = tile_bytes.getvalue()
responses.add(
responses.GET,
"http://nowhere/info.json",
json={"width": 543, "height": 720, "tiles": [{"width": 181, "height": 240}]},
)
for x in (0, 181, 362):
for y in (0, 240, 480):
responses.add(
responses.GET,
f"http://nowhere/{x},{y},181,240/full/0/default.jpg",
body=tile_bytes,
)
actual = download_tiles("http://nowhere")
assert _root_mean_square(expected, actual) <= 5.0
def test_download_tiles_small(responses):
small_tile = BytesIO()
Image.new("RGB", (1, 1)).save(small_tile, format="jpeg")
small_tile.seek(0)
responses.add(
responses.GET,
"http://nowhere/info.json",
json={"width": 543, "height": 720, "tiles": [{"width": 181, "height": 240}]},
)
responses.add(
responses.GET,
"http://nowhere/0,0,181,240/full/0/default.jpg",
body=small_tile.read(),
)
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"