From 8869724b51de7fb241159dc8546d4c4b4d16c6ae Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Sun, 7 Jul 2013 22:41:04 -0500 Subject: [PATCH] Remove color and size from Picture.__init__ Also rename `from_color` to `from_size`. --- skimage/novice/_novice.py | 50 +++++++++-------------------- skimage/novice/tests/test_novice.py | 12 +++---- 2 files changed, 21 insertions(+), 41 deletions(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index 78f3a4f3..6ab629ad 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -6,8 +6,8 @@ import numpy as np from skimage import io from skimage import img_as_ubyte from skimage.transform import resize - from skimage.color import color_dict +from skimage._shared import six # Convert colors from `skimage.color` to uint8 and allow access through @@ -291,37 +291,19 @@ class Picture(object): >>> pic[:, pic.height-1] = (255, 0, 0) """ - def __init__(self, path=None, array=None, size=None, color=None): - # Can only provide either path or size, but not both. - if (path and size) or (path and array) or (size and array): - assert False, "Can only provide path, size, or array." - - # Opening a particular file. Convert the image to RGB - # automatically so (r, g, b) tuples can be used - # everywhere. + def __init__(self, path=None, array=None): + if path is not None and array is not None: + ValueError("Only provide path or array not both.") elif path is not None: self._array = img_as_ubyte(io.imread(path)) self._path = path self._format = imghdr.what(path) - - # Creating a particular size of array. - elif size is not None: - if color is None: - color = (0, 0, 0) - - # skimage dimensions are flipped: y, x - self._array = np.zeros((size[1], size[0], 3), "uint8") - self._array[:, :] = color - self._path = None - self._format = None elif array is not None: self._array = array self._path = None self._format = None - - # Must have provided 'path', 'size', or 'array'. else: - assert False, "Must provide one of path, size, or array." + ValueError("Must provide path or array.") # Common setup. self._modified = False @@ -345,24 +327,22 @@ class Picture(object): return Picture(path=path) @staticmethod - def from_color(color, size): - """Creates a single color Picture. + def from_size(size, color='black'): + """Return a Picture of the specified size and a uniform color. Parameters ---------- - color : tuple - RGB tuple with the fill color for the picture [0-255] - size : tuple Width and height of the picture in pixels. - - Returns - ------- - pic : Picture - A Picture with the given color and size. - + color : tuple or str + RGB tuple with the fill color for the picture [0-255] or a valid + key in `color_dict`. """ - return Picture(color=color, size=size) + if isinstance(color, six.string_types): + color = color_dict[color] + rgb_size = tuple(size) + (3,) + array = np.ones(rgb_size, dtype=np.uint8) * color + return Picture(array=array) @staticmethod def from_array(array): diff --git a/skimage/novice/tests/test_novice.py b/skimage/novice/tests/test_novice.py index 395dc5e0..9e2e28e4 100644 --- a/skimage/novice/tests/test_novice.py +++ b/skimage/novice/tests/test_novice.py @@ -53,7 +53,7 @@ def test_modify(): def test_pixel_rgb(): - pic = novice.Picture(size=(3, 3), color=(10, 10, 10)) + pic = novice.Picture.from_size((3, 3), color=(10, 10, 10)) pixel = pic[0, 0] pixel.rgb = range(3) @@ -71,32 +71,32 @@ def test_pixel_rgb(): def test_pixel_rgb_float(): - pixel = novice.Picture(size=(1, 1))[0, 0] + pixel = novice.Picture.from_size((1, 1))[0, 0] pixel.rgb = (1.1, 1.1, 1.1) assert_equal(pixel.rgb, (1, 1, 1)) @raises(ValueError) def test_pixel_rgb_raises(): - pixel = novice.Picture(size=(1, 1))[0, 0] + pixel = novice.Picture.from_size((1, 1))[0, 0] pixel.rgb = (-1, -1, -1) @raises(ValueError) def test_pixel_red_raises(): - pixel = novice.Picture(size=(1, 1))[0, 0] + pixel = novice.Picture.from_size((1, 1))[0, 0] pixel.red = 256 @raises(ValueError) def test_pixel_green_raises(): - pixel = novice.Picture(size=(1, 1))[0, 0] + pixel = novice.Picture.from_size((1, 1))[0, 0] pixel.green = 256 @raises(ValueError) def test_pixel_blue_raises(): - pixel = novice.Picture(size=(1, 1))[0, 0] + pixel = novice.Picture.from_size((1, 1))[0, 0] pixel.blue = 256