Skip to content
This repository has been archived by the owner on Jul 2, 2021. It is now read-only.

Commit

Permalink
wip test
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyu2172 committed Feb 3, 2019
1 parent 0ab431d commit ca4c25a
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions tests/utils_tests/image_tests/test_read_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import tempfile
import unittest

from PIL import Image

import chainer
from chainer import testing

Expand All @@ -15,13 +17,40 @@
_cv2_available = False


@testing.parameterize(*testing.product({
'size': [(48, 32)],
'color': [True, False],
'suffix': ['bmp', 'jpg', 'png'],
'dtype': [np.float32, np.uint8, bool],
'backend': ['cv2', 'PIL'],
}))
def _write_rgba_image(rgba, path):
rgba = rgba.transpose((1, 2, 0))
rgba = Image.fromarray(rgba, 'RGBA')
canvas = Image.new('RGBA', rgba.size, (255,255,255,255))
# Paste the image onto the canvas, using it's alpha channel as mask
canvas.paste(rgba, mask=rgba)
canvas.save(path)


def _create_parameters():
params = testing.product({
'size': [(48, 32)],
'dtype': [np.float32, np.uint8, bool],
'backend': ['cv2', 'PIL']})
no_color_params = testing.product({
'color': [False],
'alpha': [None],
'suffix': ['bmp', 'jpg', 'png']})
no_alpha_params = testing.product({
'color': [True],
'alpha': [None],
'suffix': ['bmp', 'jpg', 'png']})
alpha_params = testing.product({
'color': [True],
'alpha': ['ignore', 'blend_with_white', 'blend_with_black'],
'suffix': ['png'] # writing alpha image with jpg encoding didn't work
})
params = testing.product_dict(
params,
no_color_params + no_alpha_params + alpha_params)
return params


@testing.parameterize(*_create_parameters())
class TestReadImage(unittest.TestCase):

def setUp(self):
Expand All @@ -31,36 +60,42 @@ def setUp(self):
suffix='.' + self.suffix, delete=False)
self.path = self.file.name

if self.color:
self.img = np.random.randint(
0, 255, size=(3,) + self.size, dtype=np.uint8)
if self.alpha is None:
if self.color:
self.img = np.random.randint(
0, 255, size=(3,) + self.size, dtype=np.uint8)
else:
self.img = np.random.randint(
0, 255, size=(1,) + self.size, dtype=np.uint8)
write_image(self.img, self.path)
else:
self.img = np.random.randint(
0, 255, size=(1,) + self.size, dtype=np.uint8)
write_image(self.img, self.path)
0, 255, size=(4,) + self.size, dtype=np.uint8)
_write_rgba_image(self.img, self.path)


def test_read_image_as_color(self):
img = read_image(self.path, dtype=self.dtype)
img = read_image(self.path, dtype=self.dtype, alpha=self.alpha)

self.assertEqual(img.shape, (3,) + self.size)
self.assertEqual(img.dtype, self.dtype)

if self.suffix in {'bmp', 'png'}:
if self.suffix in {'bmp', 'png'} and self.alpha is None:
np.testing.assert_equal(
img,
np.broadcast_to(self.img, (3,) + self.size).astype(self.dtype))

def test_read_image_as_grayscale(self):
img = read_image(self.path, dtype=self.dtype, color=False)
img = read_image(self.path, dtype=self.dtype, color=False, alpha=self.alpha)

self.assertEqual(img.shape, (1,) + self.size)
self.assertEqual(img.dtype, self.dtype)

if self.suffix in {'bmp', 'png'} and not self.color:
if self.suffix in {'bmp', 'png'} and not self.color and self.alpha is None:
np.testing.assert_equal(img, self.img.astype(self.dtype))

def test_read_image_mutable(self):
img = read_image(self.path)
img = read_image(self.path, alpha=self.alpha)
img[:] = 0
np.testing.assert_equal(img, 0)

Expand Down

0 comments on commit ca4c25a

Please sign in to comment.