Skip to content

Commit 1e2bd59

Browse files
Merge pull request #665 from adamtheturtle/image-file-util
Move image file helper to utils
2 parents ca5e367 + b3cf272 commit 1e2bd59

File tree

2 files changed

+61
-43
lines changed

2 files changed

+61
-43
lines changed

tests/mock_vws/fixtures/images.py

Lines changed: 22 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 make_image_file
4711

4812

4913
@pytest.fixture
@@ -52,23 +16,38 @@ 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 make_image_file(
20+
file_format='PNG',
21+
color_space='RGB',
22+
width=5,
23+
height=5,
24+
)
5625

5726

5827
@pytest.fixture
5928
def png_rgb() -> io.BytesIO:
6029
"""
6130
Return a 1x1 PNG file in the RGB color space.
6231
"""
63-
return _image_file(file_format='PNG', color_space='RGB', width=1, height=1)
32+
return make_image_file(
33+
file_format='PNG',
34+
color_space='RGB',
35+
width=1,
36+
height=1,
37+
)
6438

6539

6640
@pytest.fixture
6741
def png_greyscale() -> io.BytesIO:
6842
"""
6943
Return a 1x1 PNG file in the greyscale color space.
7044
"""
71-
return _image_file(file_format='PNG', color_space='L', width=1, height=1)
45+
return make_image_file(
46+
file_format='PNG',
47+
color_space='L',
48+
width=1,
49+
height=1,
50+
)
7251

7352

7453
@pytest.fixture()
@@ -98,7 +77,7 @@ def jpeg_cmyk() -> io.BytesIO:
9877
"""
9978
Return a 1x1 JPEG file in the CMYK color space.
10079
"""
101-
return _image_file(
80+
return make_image_file(
10281
file_format='JPEG',
10382
color_space='CMYK',
10483
width=1,
@@ -111,7 +90,7 @@ def jpeg_rgb() -> io.BytesIO:
11190
"""
11291
Return a 1x1 JPEG file in the RGB color space.
11392
"""
114-
return _image_file(
93+
return make_image_file(
11594
file_format='JPEG',
11695
color_space='RGB',
11796
width=1,
@@ -127,7 +106,7 @@ def tiff_rgb() -> io.BytesIO:
127106
This is given as an option which is not supported by Vuforia as Vuforia
128107
supports only JPEG and PNG files.
129108
"""
130-
return _image_file(
109+
return make_image_file(
131110
file_format='TIFF',
132111
color_space='RGB',
133112
width=1,

tests/mock_vws/utils/__init__.py

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

5+
import io
6+
import random
57
import json
68
from time import sleep
79
from typing import Any, Dict
810
from urllib.parse import urljoin
911

1012
import requests
1113
import timeout_decorator
14+
from PIL import Image
1215
from requests import Response
1316
from requests_mock import DELETE, GET, POST, PUT
1417
from urllib3.filepost import encode_multipart_formdata
@@ -417,3 +420,39 @@ def query(
417420
)
418421

419422
return response
423+
424+
425+
def make_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)