Skip to content

Commit 9cf4d15

Browse files
Merge pull request #668 from adamtheturtle/max-image-size-jpg
Max image size jpg
2 parents 05c1146 + 74e90e5 commit 9cf4d15

File tree

3 files changed

+76
-16
lines changed

3 files changed

+76
-16
lines changed

spelling_private_dict.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ io
2626
issuecomment
2727
jpeg
2828
json
29+
kib
2930
kwargs
3031
linters
3132
mb
3233
metadata
34+
mib
3335
mockvws
3436
multipart
3537
noqa

src/mock_vws/_mock_web_query_api.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,12 @@ def validate_image_file_size(
105105

106106
[image] = parsed['image']
107107

108-
image_file = io.BytesIO(image)
109-
pil_image = Image.open(image_file)
110-
111-
if pil_image.format != 'PNG':
112-
return wrapped(*args, **kwargs)
113-
114-
documented_max_png_bytes = 2 * 1024 * 1024
115-
if len(image) > documented_max_png_bytes:
108+
# This is the documented maximum size of a PNG as per.
109+
# https://library.vuforia.com/articles/Solution/How-To-Perform-an-Image-Recognition-Query.
110+
# However, the tests show that this maximum size also applies to JPEG
111+
# files.
112+
max_bytes = 2 * 1024 * 1024
113+
if len(image) > max_bytes:
116114
raise requests.exceptions.ConnectionError
117115
return wrapped(*args, **kwargs)
118116

tests/mock_vws/test_query.py

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,7 @@ def test_png(
10791079
We do not test exactly at this limit, but that may be beneficial in the
10801080
future.
10811081
"""
1082-
documented_max_bytes = 2 * 1024 * 1024
1082+
max_bytes = 2 * 1024 * 1024
10831083
width = height = 835
10841084
png_not_too_large = make_image_file(
10851085
file_format='PNG',
@@ -1096,8 +1096,8 @@ def test_png(
10961096
# maximum file size.
10971097
#
10981098
# This is just because of the implementation details of ``image_file``.
1099-
assert image_content_size < documented_max_bytes
1100-
assert (image_content_size * 1.05) > documented_max_bytes
1099+
assert image_content_size < max_bytes
1100+
assert (image_content_size * 1.05) > max_bytes
11011101

11021102
response = query(
11031103
vuforia_database_keys=vuforia_database_keys,
@@ -1122,20 +1122,80 @@ def test_png(
11221122
# maximum file size.
11231123
#
11241124
# This is just because of the implementation details of ``image_file``.
1125-
assert image_content_size > documented_max_bytes
1126-
assert (image_content_size * 0.95) < documented_max_bytes
1125+
assert image_content_size > max_bytes
1126+
assert (image_content_size * 0.95) < max_bytes
11271127

11281128
with pytest.raises(requests.exceptions.ConnectionError):
11291129
query(
11301130
vuforia_database_keys=vuforia_database_keys,
11311131
body=body,
11321132
)
11331133

1134-
def test_jpeg(self) -> None:
1134+
def test_jpeg(
1135+
self,
1136+
vuforia_database_keys: VuforiaDatabaseKeys,
1137+
) -> None:
11351138
"""
1136-
See https://github.com/adamtheturtle/vws-python/issues/357 for
1137-
implementing this test.
1139+
According to
1140+
https://library.vuforia.com/articles/Solution/How-To-Perform-an-Image-Recognition-Query.
1141+
the maximum file size is "512 KiB for JPEG".
1142+
However, this test shows that the maximum size for JPEG is 2 MiB.
1143+
1144+
Above this limit, a ``ConnectionError`` is raised.
1145+
We do not test exactly at this limit, but that may be beneficial in the
1146+
future.
11381147
"""
1148+
max_bytes = 2 * 1024 * 1024
1149+
width = height = 1864
1150+
png_not_too_large = make_image_file(
1151+
file_format='JPEG',
1152+
color_space='RGB',
1153+
width=width,
1154+
height=height,
1155+
)
1156+
1157+
image_content = png_not_too_large.getvalue()
1158+
body = {'image': ('image.jpeg', image_content, 'image/jpeg')}
1159+
1160+
image_content_size = len(image_content)
1161+
# We check that the image we created is just slightly smaller than the
1162+
# maximum file size.
1163+
#
1164+
# This is just because of the implementation details of ``image_file``.
1165+
assert image_content_size < max_bytes
1166+
assert (image_content_size * 1.05) > max_bytes
1167+
1168+
response = query(
1169+
vuforia_database_keys=vuforia_database_keys,
1170+
body=body,
1171+
)
1172+
1173+
assert_query_success(response=response)
1174+
assert response.json()['results'] == []
1175+
1176+
width = height = 1865
1177+
png_not_too_large = make_image_file(
1178+
file_format='JPEG',
1179+
color_space='RGB',
1180+
width=width,
1181+
height=height,
1182+
)
1183+
1184+
image_content = png_not_too_large.getvalue()
1185+
body = {'image': ('image.jpeg', image_content, 'image/jpeg')}
1186+
image_content_size = len(image_content)
1187+
# We check that the image we created is just slightly larger than the
1188+
# maximum file size.
1189+
#
1190+
# This is just because of the implementation details of ``image_file``.
1191+
assert image_content_size > max_bytes
1192+
assert (image_content_size * 0.95) < max_bytes
1193+
1194+
with pytest.raises(requests.exceptions.ConnectionError):
1195+
query(
1196+
vuforia_database_keys=vuforia_database_keys,
1197+
body=body,
1198+
)
11391199

11401200

11411201
@pytest.mark.usefixtures('verify_mock_vuforia')

0 commit comments

Comments
 (0)