-
Notifications
You must be signed in to change notification settings - Fork 618
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code to import same decorator
Add `requires_img_lib` decorator and use it in test_image and test_dataio. Update test_image to reduce redundancies.
- Loading branch information
Showing
4 changed files
with
29 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from ._testing import (SkipTest, requires_application, requires_pyopengl, # noqa | ||
from ._testing import (SkipTest, requires_application, requires_img_lib, # noqa | ||
assert_is, assert_in, assert_not_in, has_backend, # noqa | ||
glut_skip) # noqa | ||
glut_skip, requires_pyopengl) # noqa | ||
from ._runners import _tester # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,33 @@ | ||
import numpy as np | ||
from vispy.util import make_png | ||
from vispy.util.dataio import _check_img_lib, imread | ||
from numpy.testing import assert_array_equal | ||
from os import path as op | ||
|
||
has_img_lib = not all(c is None for c in _check_img_lib()) | ||
requires_img_lib = np.testing.dec.skipif(not has_img_lib, 'imageio or PIL ' | ||
'required') | ||
from vispy.util import make_png, _TempDir | ||
from vispy.util.dataio import imread | ||
from vispy.testing import requires_img_lib | ||
|
||
temp_dir = _TempDir() | ||
|
||
@requires_img_lib | ||
|
||
@requires_img_lib() | ||
def test_make_png(): | ||
""" Test to ensure that make_png functions correctly. | ||
Save random RGBA and RGB arrays onto disk as PNGs using make_png. | ||
Read them back with an image library and check whether the array | ||
saved is equal to the array read. | ||
""" | ||
|
||
## RGBA | ||
# Create random RGBA array as type ubyte | ||
rgba_save = np.random.randint(256, size=(100, 100, 4)).astype(np.ubyte) | ||
# Save RGBA with make_png | ||
with open('rgba.png', 'wb') as f1: | ||
f1.write(make_png(rgba_save)) | ||
# Read back and check | ||
rgba_read = imread('rgba.png', 'PNG') | ||
assert_array_equal(rgba_save, rgba_read) | ||
|
||
## RGB | ||
# Get rid of the alpha for RGB | ||
rgb_save = rgba_save[:, :, :3] | ||
# Save RGB with make_png | ||
with open('rgb.png', 'wb') as f2: | ||
f2.write(make_png(rgb_save)) | ||
# Read back and check | ||
rgb_read = imread('rgb.png', 'PNG') | ||
assert_array_equal(rgb_save, rgb_read) | ||
# Output file should be in temp | ||
png_out = op.join(temp_dir, 'random.png') | ||
|
||
for rgb_a in (rgba_save, rgb_save): | ||
with open(png_out, 'wb') as f: | ||
f.write(make_png(rgb_a)) # Save array with make_png | ||
|
||
# Read back with a library and check equality | ||
rgb_a_read = imread(png_out, 'PNG') | ||
assert_array_equal(rgb_a, rgb_a_read) |