Skip to content

Commit e94ec40

Browse files
committed
start of moving around image file util
1 parent 084606d commit e94ec40

File tree

3 files changed

+47
-64
lines changed

3 files changed

+47
-64
lines changed

tests/mock_vws/fixtures/images.py

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,11 @@
33
"""
44

55
import io
6-
import random
76

87
import pytest
98
from _pytest.fixtures import SubRequest
10-
from PIL import Image
119

12-
13-
def _image_file(
14-
file_format: str,
15-
color_space: str,
16-
width: int,
17-
height: int,
18-
) -> io.BytesIO:
19-
"""
20-
Return an image file in the given format and color space.
21-
22-
The image file is filled with randomly colored pixels.
23-
24-
Args:
25-
file_format: See
26-
http://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html
27-
color_space: One of "L", "RGB", or "CMYK". "L" means greyscale.
28-
width: The width, in pixels of the image.
29-
height: The width, in pixels of the image.
30-
31-
Returns:
32-
An image file in the given format and color space.
33-
"""
34-
image_buffer = io.BytesIO()
35-
image = Image.new(color_space, (width, height))
36-
pixels = image.load()
37-
for i in range(height):
38-
for j in range(width):
39-
red = random.randint(0, 255)
40-
green = random.randint(0, 255)
41-
blue = random.randint(0, 255)
42-
if color_space != 'L':
43-
pixels[j, i] = (red, green, blue)
44-
image.save(image_buffer, file_format)
45-
image_buffer.seek(0)
46-
return image_buffer
10+
from tests.mock_vws.utils import image_file
4711

4812

4913
@pytest.fixture
@@ -52,23 +16,23 @@ def png_rgb_success() -> io.BytesIO:
5216
Return a PNG file in the RGB color space which is expected to have a
5317
'success' status when added to a target.
5418
"""
55-
return _image_file(file_format='PNG', color_space='RGB', width=5, height=5)
19+
return image_file(file_format='PNG', color_space='RGB', width=5, height=5)
5620

5721

5822
@pytest.fixture
5923
def png_rgb() -> io.BytesIO:
6024
"""
6125
Return a 1x1 PNG file in the RGB color space.
6226
"""
63-
return _image_file(file_format='PNG', color_space='RGB', width=1, height=1)
27+
return image_file(file_format='PNG', color_space='RGB', width=1, height=1)
6428

6529

6630
@pytest.fixture
6731
def png_greyscale() -> io.BytesIO:
6832
"""
6933
Return a 1x1 PNG file in the greyscale color space.
7034
"""
71-
return _image_file(file_format='PNG', color_space='L', width=1, height=1)
35+
return image_file(file_format='PNG', color_space='L', width=1, height=1)
7236

7337

7438
@pytest.fixture()
@@ -98,7 +62,7 @@ def jpeg_cmyk() -> io.BytesIO:
9862
"""
9963
Return a 1x1 JPEG file in the CMYK color space.
10064
"""
101-
return _image_file(
65+
return image_file(
10266
file_format='JPEG',
10367
color_space='CMYK',
10468
width=1,
@@ -111,7 +75,7 @@ def jpeg_rgb() -> io.BytesIO:
11175
"""
11276
Return a 1x1 JPEG file in the RGB color space.
11377
"""
114-
return _image_file(
78+
return image_file(
11579
file_format='JPEG',
11680
color_space='RGB',
11781
width=1,
@@ -127,7 +91,7 @@ def tiff_rgb() -> io.BytesIO:
12791
This is given as an option which is not supported by Vuforia as Vuforia
12892
supports only JPEG and PNG files.
12993
"""
130-
return _image_file(
94+
return image_file(
13195
file_format='TIFF',
13296
color_space='RGB',
13397
width=1,

tests/mock_vws/test_query.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import calendar
99
import datetime
1010
import io
11-
import math
12-
import random
1311
import time
1412
from typing import Dict, Union
1513
from urllib.parse import urljoin
@@ -1067,10 +1065,6 @@ class TestMaximumImageSize:
10671065
Tests for maximum image sizes.
10681066
"""
10691067

1070-
def _png(width: int, height: int):
1071-
"""
1072-
XXX
1073-
"""
10741068
def test_png(
10751069
self,
10761070
vuforia_database_keys: VuforiaDatabaseKeys,
@@ -1079,28 +1073,14 @@ def test_png(
10791073
See https://github.com/adamtheturtle/vws-python/issues/357 for
10801074
implementing this test.
10811075
"""
1082-
file_format = 'PNG'
1083-
color_space = 'RGB'
10841076
# 835 no error, 836 error
10851077
width = height = 836
1086-
width = height = 835
1078+
# width = height = 835
10871079

10881080
# # This gives 422 on real, 200 on mock
10891081
# width = 1
10901082
# height = int(max_size / 2)
10911083

1092-
image_buffer = io.BytesIO()
1093-
image = Image.new(color_space, (width, height))
1094-
pixels = image.load()
1095-
for i in range(height):
1096-
for j in range(width):
1097-
red = random.randint(0, 255)
1098-
green = random.randint(0, 255)
1099-
blue = random.randint(0, 255)
1100-
pixels[j, i] = (red, green, blue)
1101-
image.save(image_buffer, file_format)
1102-
image_buffer.seek(0)
1103-
11041084
image_content = image_buffer.getvalue()
11051085
body = {'image': ('image.jpeg', image_content, 'image/jpeg')}
11061086

tests/mock_vws/utils/__init__.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
Utilities for tests.
33
"""
44

5+
import io
6+
from PIL import Image
7+
import random
58
import json
69
from time import sleep
710
from typing import Any, Dict
@@ -417,3 +420,39 @@ def query(
417420
)
418421

419422
return response
423+
424+
425+
def image_file(
426+
file_format: str,
427+
color_space: str,
428+
width: int,
429+
height: int,
430+
) -> io.BytesIO:
431+
"""
432+
Return an image file in the given format and color space.
433+
434+
The image file is filled with randomly colored pixels.
435+
436+
Args:
437+
file_format: See
438+
http://pillow.readthedocs.io/en/3.1.x/handbook/image-file-formats.html
439+
color_space: One of "L", "RGB", or "CMYK". "L" means greyscale.
440+
width: The width, in pixels of the image.
441+
height: The width, in pixels of the image.
442+
443+
Returns:
444+
An image file in the given format and color space.
445+
"""
446+
image_buffer = io.BytesIO()
447+
image = Image.new(color_space, (width, height))
448+
pixels = image.load()
449+
for i in range(height):
450+
for j in range(width):
451+
red = random.randint(0, 255)
452+
green = random.randint(0, 255)
453+
blue = random.randint(0, 255)
454+
if color_space != 'L':
455+
pixels[j, i] = (red, green, blue)
456+
image.save(image_buffer, file_format)
457+
image_buffer.seek(0)
458+
return image_buffer

0 commit comments

Comments
 (0)