From f5c68531ab11891bdc3a221894438eb8eea83ac4 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Tue, 22 Oct 2013 13:07:16 -0400 Subject: [PATCH 1/8] Novice module can open images from a url --- skimage/novice/_novice.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index 2fc4b280..fbae0768 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -1,6 +1,10 @@ import os import imghdr +import shutil from collections import namedtuple +from io import BytesIO +from urlparse import urlparse +from urllib2 import urlopen import numpy as np from skimage import io @@ -19,7 +23,7 @@ colors = namedtuple('colors', color_dict.keys())(**color_dict) def open(path): """Return Picture object from the given image path.""" - return Picture(path=os.path.abspath(path)) + return Picture(path=path) def _verify_picture_index(index): @@ -212,7 +216,16 @@ class Picture(object): elif path is not None: self.array = img_as_ubyte(io.imread(path)) self._path = path - self._format = imghdr.what(path) + + # Copy to in-memory buffer to get format (in case path was a url) + if urlparse(path).scheme == "": + self._format = imghdr.what(path) + else: + data = BytesIO() + shutil.copyfileobj(urlopen(path), data) + data.seek(0) + self._format = imghdr.what(data) + del data elif array is not None: self.array = array elif xy_array is not None: From 9d91751ed965875839c3186aa6859af4b8fbd21f Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Wed, 23 Oct 2013 15:44:50 -0400 Subject: [PATCH 2/8] Switching to RGBA --- skimage/novice/_novice.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index fbae0768..df2c3fdc 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -148,7 +148,8 @@ class Pixel(object): return value def _setpixel(self): - self._picture.xy_array[self._x, self._y] = self.rgb + # RGB + alpha + self._picture.xy_array[self._x, self._y] = self.rgb + (255,) self._picture._array_modified() def __eq__(self, other): @@ -231,6 +232,10 @@ class Picture(object): elif xy_array is not None: self.xy_array = xy_array + # Force RGBA internally (max alpha) + if self.array.shape[-1] == 3: + self.array = np.insert(self.array, 3, values=255, axis=2) + @staticmethod def from_size(size, color='black'): """Return a Picture of the specified size and a uniform color. @@ -247,6 +252,7 @@ class Picture(object): color = color_dict[color] rgb_size = tuple(size) + (3,) array = np.ones(rgb_size, dtype=np.uint8) * color + array = np.insert(array, 3, values=255, axis=2) return Picture(array=array) @property From 1d6531aa045e92cc31de4f2979d6de2e27d4cdbd Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Wed, 23 Oct 2013 16:28:41 -0400 Subject: [PATCH 3/8] Fixed Python 3 import errors, added alpha and rgba properties --- skimage/novice/_novice.py | 93 ++++++++++++++++++++++------- skimage/novice/tests/test_novice.py | 48 ++++++++++++--- 2 files changed, 111 insertions(+), 30 deletions(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index df2c3fdc..fb593a69 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -1,10 +1,7 @@ import os import imghdr -import shutil from collections import namedtuple from io import BytesIO -from urlparse import urlparse -from urllib2 import urlopen import numpy as np from skimage import io @@ -13,6 +10,10 @@ from skimage.transform import resize from skimage.color import color_dict from skimage._shared import six +from six.moves.urllib_parse import urlparse +from six.moves.urllib import request +urlopen = request.urlopen + # Convert colors from `skimage.color` to uint8 and allow access through # dict or a named tuple. @@ -75,15 +76,18 @@ class Pixel(object): Vertical coordinate of this pixel (bottom = 0). rgb : tuple RGB tuple with red, green, and blue components (0-255) + alpha : int + Transparency component (0-255), 255 (opaque) by default """ - def __init__(self, pic, array, x, y, rgb): + def __init__(self, pic, array, x, y, rgb, alpha=255): self._picture = pic self._x = x self._y = y self._red = self._validate(rgb[0]) self._green = self._validate(rgb[1]) self._blue = self._validate(rgb[2]) + self._alpha = self._validate(alpha) @property def x(self): @@ -125,6 +129,16 @@ class Pixel(object): self._blue = self._validate(value) self._setpixel() + @property + def alpha(self): + """The transparency component of the pixel (0-255).""" + return self._alpha + + @alpha.setter + def alpha(self, value): + self._alpha = self._validate(value) + self._setpixel() + @property def rgb(self): """The RGB color components of the pixel (3 values 0-255).""" @@ -132,7 +146,21 @@ class Pixel(object): @rgb.setter def rgb(self, value): - self._red, self._green, self._blue = (self._validate(v) for v in value) + if len(value) == 4: + self.rgba = value + else: + self._red, self._green, self._blue = (self._validate(v) for v in value) + self._alpha = 255 + self._setpixel() + + @property + def rgba(self): + """The RGB color and transparency components of the pixel (4 values 0-255).""" + return (self.red, self.green, self.blue, self.alpha) + + @rgba.setter + def rgba(self, value): + self._red, self._green, self._blue, self._alpha = (self._validate(v) for v in value) self._setpixel() def _validate(self, value): @@ -149,16 +177,16 @@ class Pixel(object): def _setpixel(self): # RGB + alpha - self._picture.xy_array[self._x, self._y] = self.rgb + (255,) + self._picture.xy_array[self._x, self._y] = self.rgba self._picture._array_modified() def __eq__(self, other): if isinstance(other, Pixel): - return self.rgb == other.rgb + return self.rgba == other.rgba def __repr__(self): - args = self.red, self.green, self.blue - return "Pixel(red={0}, green={1}, blue={2})".format(*args) + args = self.red, self.green, self.blue, self.alpha + return "Pixel(red={0}, green={1}, blue={2}, alpha={3})".format(*args) class Picture(object): @@ -169,9 +197,9 @@ class Picture(object): path : str Path to an image file to load. array : array - Raw RGB image data [0-255], with origin at top-left. + Raw RGB or RGBA image data [0-255], with origin at top-left. xy_array : array - Raw RGB image data [0-255], with origin at bottom-left. + Raw RGB or RGBA image data [0-255], with origin at bottom-left. Examples -------- @@ -215,24 +243,21 @@ class Picture(object): msg = "Must provide a single keyword arg (path, array, xy_array)." ValueError(msg) elif path is not None: - self.array = img_as_ubyte(io.imread(path)) self._path = path - # Copy to in-memory buffer to get format (in case path was a url) if urlparse(path).scheme == "": + self.array = img_as_ubyte(io.imread(path)) self._format = imghdr.what(path) else: - data = BytesIO() - shutil.copyfileobj(urlopen(path), data) - data.seek(0) - self._format = imghdr.what(data) - del data + data = urlopen(path).read() + self.array = img_as_ubyte(io.imread(BytesIO(data))) + self._format = imghdr.what("", h=data) elif array is not None: self.array = array elif xy_array is not None: self.xy_array = xy_array - # Force RGBA internally (max alpha) + # Force RGBA internally (use max alpha) if self.array.shape[-1] == 3: self.array = np.insert(self.array, 3, values=255, axis=2) @@ -245,14 +270,18 @@ class Picture(object): size : tuple Width and height of the picture in pixels. color : tuple or str - RGB tuple with the fill color for the picture [0-255] or a valid + RGB or RGBA tuple with the fill color for the picture [0-255] or a valid key in `color_dict`. """ if isinstance(color, six.string_types): color = color_dict[color] - rgb_size = tuple(size) + (3,) + rgb_size = tuple(size) + (len(color),) array = np.ones(rgb_size, dtype=np.uint8) * color - array = np.insert(array, 3, values=255, axis=2) + + # Force RGBA internally (use max alpha) + if array.shape[-1] == 3: + array = np.insert(array, 3, values=255, axis=2) + return Picture(array=array) @property @@ -396,13 +425,31 @@ class Picture(object): def blue(self, value): self._set_channel(2, value) + @property + def alpha(self): + """The transparency component of the pixel (0-255).""" + return self._get_channel(3).ravel() + + @alpha.setter + def alpha(self, value): + self._set_channel(3, value) + @property def rgb(self): """The RGB color components of the pixel (3 values 0-255).""" - return self.xy_array + return self.xy_array[:, :, :3] @rgb.setter def rgb(self, value): + self.xy_array[:, :, :3] = value + + @property + def rgba(self): + """The RGBA color components of the pixel (4 values 0-255).""" + return self.xy_array + + @rgba.setter + def rgba(self, value): self.xy_array[:] = value def __iter__(self): diff --git a/skimage/novice/tests/test_novice.py b/skimage/novice/tests/test_novice.py index 80065e46..da0008fc 100644 --- a/skimage/novice/tests/test_novice.py +++ b/skimage/novice/tests/test_novice.py @@ -13,8 +13,8 @@ IMAGE_PATH = os.path.join(data_dir, "chelsea.png") SMALL_IMAGE_PATH = os.path.join(data_dir, "block.png") -def _array_2d_to_RGB(array): - return np.tile(array[:, :, np.newaxis], (1, 1, 3)) +def _array_2d_to_RGBA(array): + return np.tile(array[:, :, np.newaxis], (1, 1, 4)) def test_xy_to_array_origin(): @@ -82,6 +82,28 @@ def test_pixel_rgb(): for i, channel in enumerate((pixel.red, pixel.green, pixel.blue)): assert_equal(channel, i + 3) + pixel.rgb = np.arange(4) + assert_equal(pixel.rgb, np.arange(3)) + + +def test_pixel_rgba(): + pic = novice.Picture.from_size((3, 3), color=(10, 10, 10)) + pixel = pic[0, 0] + pixel.rgba = np.arange(4) + + assert_equal(pixel.rgba, np.arange(4)) + for i, channel in enumerate((pixel.red, pixel.green, pixel.blue, pixel.alpha)): + assert_equal(channel, i) + + pixel.red = 3 + pixel.green = 4 + pixel.blue = 5 + pixel.alpha = 6 + assert_equal(pixel.rgba, np.arange(4) + 3) + + for i, channel in enumerate((pixel.red, pixel.green, pixel.blue, pixel.alpha)): + assert_equal(channel, i + 3) + def test_pixel_rgb_float(): pixel = novice.Picture.from_size((1, 1))[0, 0] @@ -89,6 +111,12 @@ def test_pixel_rgb_float(): assert_equal(pixel.rgb, (1, 1, 1)) +def test_pixel_rgba_float(): + pixel = novice.Picture.from_size((1, 1))[0, 0] + pixel.rgba = (1.1, 1.1, 1.1, 1.1) + assert_equal(pixel.rgba, (1, 1, 1, 1)) + + def test_modified_on_set(): pic = novice.Picture(SMALL_IMAGE_PATH) pic[0, 0] = (1, 1, 1) @@ -157,7 +185,7 @@ def test_indexing(): def test_picture_slice(): - array = _array_2d_to_RGB(np.arange(0, 10)[np.newaxis, :]) + array = _array_2d_to_RGBA(np.arange(0, 10)[np.newaxis, :]) pic = novice.Picture(array=array) x_slice = slice(3, 8) @@ -167,7 +195,7 @@ def test_picture_slice(): def test_move_slice(): h, w = 3, 12 - array = _array_2d_to_RGB(np.linspace(0, 255, h * w).reshape(h, w)) + array = _array_2d_to_RGBA(np.linspace(0, 255, h * w).reshape(h, w)) array = array.astype(np.uint8) pic = novice.Picture(array=array) @@ -187,7 +215,7 @@ def test_move_slice(): def test_negative_index(): n = 10 - array = _array_2d_to_RGB(np.arange(0, n)[np.newaxis, :]) + array = _array_2d_to_RGBA(np.arange(0, n)[np.newaxis, :]) # Test both x and y indices. pic = novice.Picture(array=array) assert pic[-1, 0] == pic[n - 1, 0] @@ -197,7 +225,7 @@ def test_negative_index(): def test_negative_slice(): n = 10 - array = _array_2d_to_RGB(np.arange(0, n)[np.newaxis, :]) + array = _array_2d_to_RGBA(np.arange(0, n)[np.newaxis, :]) # Test both x and y slices. pic = novice.Picture(array=array) assert pic[-3:, 0] == pic[n - 3:, 0] @@ -207,7 +235,7 @@ def test_negative_slice(): def test_getitem_with_step(): h, w = 5, 5 - array = _array_2d_to_RGB(np.linspace(0, 255, h * w).reshape(h, w)) + array = _array_2d_to_RGBA(np.linspace(0, 255, h * w).reshape(h, w)) pic = novice.Picture(array=array) sliced_pic = pic[::2, ::2] assert sliced_pic == novice.Picture(array=array[::2, ::2]) @@ -267,6 +295,12 @@ def test_pixel_blue_raises(): pixel.blue = 256 +@raises(ValueError) +def test_pixel_alpha_raises(): + pixel = novice.Picture.from_size((1, 1))[0, 0] + pixel.alpha = 256 + + if __name__ == '__main__': from numpy import testing testing.run_module_suite() From 522778b2147aa6ad4cf2727b895b7905dcb7d0a0 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Mon, 28 Oct 2013 10:29:20 -0400 Subject: [PATCH 4/8] Trying to get imports working with both Python 2 and 3 --- skimage/novice/_novice.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index fb593a69..946fab15 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -10,9 +10,23 @@ from skimage.transform import resize from skimage.color import color_dict from skimage._shared import six -from six.moves.urllib_parse import urlparse -from six.moves.urllib import request -urlopen = request.urlopen +try: + from urlparse import urlparse +except ImportError: + try: + from urllib import parse as urlparse + except ImportError: + from six.moves.urllib_parse import urlparse + +try: + from urllib2 import urlopen +except ImportError: + try: + from urllib import request + urlopen = request.urlopen + except ImportError: + from six.moves.urllib import request + urlopen = request.urlopen # Convert colors from `skimage.color` to uint8 and allow access through From f11ef60c35d9e986ec77c5adb99da8c6cbe6b89d Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Tue, 29 Oct 2013 09:43:52 -0400 Subject: [PATCH 5/8] Fixed import of urlparse and rebase on upstream --- skimage/novice/_novice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index 946fab15..c582ef0e 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -14,7 +14,7 @@ try: from urlparse import urlparse except ImportError: try: - from urllib import parse as urlparse + from urllib.parse import urlparse except ImportError: from six.moves.urllib_parse import urlparse From 695da418412eb2f9c5a486542278d0c7d57ce864 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Wed, 20 Nov 2013 14:00:46 -0500 Subject: [PATCH 6/8] Removed extraneous trys for six imports --- skimage/novice/_novice.py | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index c582ef0e..fb593a69 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -10,23 +10,9 @@ from skimage.transform import resize from skimage.color import color_dict from skimage._shared import six -try: - from urlparse import urlparse -except ImportError: - try: - from urllib.parse import urlparse - except ImportError: - from six.moves.urllib_parse import urlparse - -try: - from urllib2 import urlopen -except ImportError: - try: - from urllib import request - urlopen = request.urlopen - except ImportError: - from six.moves.urllib import request - urlopen = request.urlopen +from six.moves.urllib_parse import urlparse +from six.moves.urllib import request +urlopen = request.urlopen # Convert colors from `skimage.color` to uint8 and allow access through From f871c4024f898656c8e665f715c3baa72c2620cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Wed, 19 Mar 2014 17:47:49 -0400 Subject: [PATCH 7/8] Extract changes by @synesthesiam in PR #797 --- skimage/novice/_novice.py | 83 ++++++++++++++++++++++++----- skimage/novice/tests/test_novice.py | 49 +++++++++++++++-- 2 files changed, 115 insertions(+), 17 deletions(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index e58f1b78..f3e041a2 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -72,15 +72,18 @@ class Pixel(object): Vertical coordinate of this pixel (bottom = 0). rgb : tuple RGB tuple with red, green, and blue components (0-255) + alpha : int + Transparency component (0-255), 255 (opaque) by default """ - def __init__(self, pic, array, x, y, rgb): + def __init__(self, pic, array, x, y, rgb, alpha=255): self._picture = pic self._x = x self._y = y self._red = self._validate(rgb[0]) self._green = self._validate(rgb[1]) self._blue = self._validate(rgb[2]) + self._alpha = self._validate(alpha) @property def x(self): @@ -122,6 +125,16 @@ class Pixel(object): self._blue = self._validate(value) self._setpixel() + @property + def alpha(self): + """The transparency component of the pixel (0-255).""" + return self._alpha + + @alpha.setter + def alpha(self, value): + self._alpha = self._validate(value) + self._setpixel() + @property def rgb(self): """The RGB color components of the pixel (3 values 0-255).""" @@ -129,7 +142,25 @@ class Pixel(object): @rgb.setter def rgb(self, value): - self._red, self._green, self._blue = (self._validate(v) for v in value) + if len(value) == 4: + self.rgba = value + else: + self._red, self._green, self._blue \ + = (self._validate(v) for v in value) + self._alpha = 255 + self._setpixel() + + @property + def rgba(self): + """The RGB color and transparency components of the pixel + (4 values 0-255). + """ + return (self.red, self.green, self.blue, self.alpha) + + @rgba.setter + def rgba(self, value): + self._red, self._green, self._blue, self._alpha \ + = (self._validate(v) for v in value) self._setpixel() def _validate(self, value): @@ -145,16 +176,17 @@ class Pixel(object): return value def _setpixel(self): - self._picture.xy_array[self._x, self._y] = self.rgb + # RGB + alpha + self._picture.xy_array[self._x, self._y] = self.rgba self._picture._array_modified() def __eq__(self, other): if isinstance(other, Pixel): - return self.rgb == other.rgb + return self.rgba == other.rgba def __repr__(self): - args = self.red, self.green, self.blue - return "Pixel(red={0}, green={1}, blue={2})".format(*args) + args = self.red, self.green, self.blue, self.alpha + return "Pixel(red={0}, green={1}, blue={2}, alpha={3})".format(*args) class Picture(object): @@ -165,9 +197,9 @@ class Picture(object): path : str Path to an image file to load / URL of an image array : array - Raw RGB image data [0-255], with origin at top-left. + Raw RGB or RGBA image data [0-255], with origin at top-left. xy_array : array - Raw RGB image data [0-255], with origin at bottom-left. + Raw RGB or RGBA image data [0-255], with origin at bottom-left. Examples -------- @@ -225,6 +257,10 @@ class Picture(object): elif xy_array is not None: self.xy_array = xy_array + # Force RGBA internally (use max alpha) + if self.array.shape[-1] == 3: + self.array = np.insert(self.array, 3, values=255, axis=2) + @staticmethod def from_size(size, color='black'): """Return a Picture of the specified size and a uniform color. @@ -234,13 +270,18 @@ class Picture(object): size : tuple Width and height of the picture in pixels. color : tuple or str - RGB tuple with the fill color for the picture [0-255] or a valid - key in `color_dict`. + RGB or RGBA tuple with the fill color for the picture [0-255] or + a valid key in `color_dict`. """ if isinstance(color, six.string_types): color = color_dict[color] - rgb_size = tuple(size) + (3,) + rgb_size = tuple(size) + (len(color),) array = np.ones(rgb_size, dtype=np.uint8) * color + + # Force RGBA internally (use max alpha) + if array.shape[-1] == 3: + array = np.insert(array, 3, values=255, axis=2) + return Picture(array=array) @property @@ -384,13 +425,31 @@ class Picture(object): def blue(self, value): self._set_channel(2, value) + @property + def alpha(self): + """The transparency component of the pixel (0-255).""" + return self._get_channel(3).ravel() + + @alpha.setter + def alpha(self, value): + self._set_channel(3, value) + @property def rgb(self): """The RGB color components of the pixel (3 values 0-255).""" - return self.xy_array + return self.xy_array[:, :, :3] @rgb.setter def rgb(self, value): + self.xy_array[:, :, :3] = value + + @property + def rgba(self): + """The RGBA color components of the pixel (4 values 0-255).""" + return self.xy_array + + @rgba.setter + def rgba(self, value): self.xy_array[:] = value def __iter__(self): diff --git a/skimage/novice/tests/test_novice.py b/skimage/novice/tests/test_novice.py index 6b4295b5..36a7c2da 100644 --- a/skimage/novice/tests/test_novice.py +++ b/skimage/novice/tests/test_novice.py @@ -17,6 +17,10 @@ def _array_2d_to_RGB(array): return np.tile(array[:, :, np.newaxis], (1, 1, 3)) +def _array_2d_to_RGBA(array): + return np.tile(array[:, :, np.newaxis], (1, 1, 4)) + + def test_xy_to_array_origin(): h, w = 3, 5 array = np.arange(h * w).reshape(h, w, 1) @@ -82,6 +86,28 @@ def test_pixel_rgb(): for i, channel in enumerate((pixel.red, pixel.green, pixel.blue)): assert_equal(channel, i + 3) + pixel.rgb = np.arange(4) + assert_equal(pixel.rgb, np.arange(3)) + + +def test_pixel_rgba(): + pic = novice.Picture.from_size((3, 3), color=(10, 10, 10)) + pixel = pic[0, 0] + pixel.rgba = np.arange(4) + + assert_equal(pixel.rgba, np.arange(4)) + for i, channel in enumerate((pixel.red, pixel.green, pixel.blue, pixel.alpha)): + assert_equal(channel, i) + + pixel.red = 3 + pixel.green = 4 + pixel.blue = 5 + pixel.alpha = 6 + assert_equal(pixel.rgba, np.arange(4) + 3) + + for i, channel in enumerate((pixel.red, pixel.green, pixel.blue, pixel.alpha)): + assert_equal(channel, i + 3) + def test_pixel_rgb_float(): pixel = novice.Picture.from_size((1, 1))[0, 0] @@ -89,6 +115,12 @@ def test_pixel_rgb_float(): assert_equal(pixel.rgb, (1, 1, 1)) +def test_pixel_rgba_float(): + pixel = novice.Picture.from_size((1, 1))[0, 0] + pixel.rgba = (1.1, 1.1, 1.1, 1.1) + assert_equal(pixel.rgba, (1, 1, 1, 1)) + + def test_modified_on_set(): pic = novice.Picture(SMALL_IMAGE_PATH) pic[0, 0] = (1, 1, 1) @@ -161,7 +193,7 @@ def test_indexing(): def test_picture_slice(): - array = _array_2d_to_RGB(np.arange(0, 10)[np.newaxis, :]) + array = _array_2d_to_RGBA(np.arange(0, 10)[np.newaxis, :]) pic = novice.Picture(array=array) x_slice = slice(3, 8) @@ -171,7 +203,7 @@ def test_picture_slice(): def test_move_slice(): h, w = 3, 12 - array = _array_2d_to_RGB(np.linspace(0, 255, h * w).reshape(h, w)) + array = _array_2d_to_RGBA(np.linspace(0, 255, h * w).reshape(h, w)) array = array.astype(np.uint8) pic = novice.Picture(array=array) @@ -191,7 +223,7 @@ def test_move_slice(): def test_negative_index(): n = 10 - array = _array_2d_to_RGB(np.arange(0, n)[np.newaxis, :]) + array = _array_2d_to_RGBA(np.arange(0, n)[np.newaxis, :]) # Test both x and y indices. pic = novice.Picture(array=array) assert pic[-1, 0] == pic[n - 1, 0] @@ -201,7 +233,7 @@ def test_negative_index(): def test_negative_slice(): n = 10 - array = _array_2d_to_RGB(np.arange(0, n)[np.newaxis, :]) + array = _array_2d_to_RGBA(np.arange(0, n)[np.newaxis, :]) # Test both x and y slices. pic = novice.Picture(array=array) assert pic[-3:, 0] == pic[n - 3:, 0] @@ -211,7 +243,7 @@ def test_negative_slice(): def test_getitem_with_step(): h, w = 5, 5 - array = _array_2d_to_RGB(np.linspace(0, 255, h * w).reshape(h, w)) + array = _array_2d_to_RGBA(np.linspace(0, 255, h * w).reshape(h, w)) pic = novice.Picture(array=array) sliced_pic = pic[::2, ::2] assert sliced_pic == novice.Picture(array=array[::2, ::2]) @@ -271,5 +303,12 @@ def test_pixel_blue_raises(): pixel.blue = 256 + +@raises(ValueError) +def test_pixel_alpha_raises(): + pixel = novice.Picture.from_size((1, 1))[0, 0] + pixel.alpha = 256 + + if __name__ == '__main__': np.testing.run_module_suite() From f84cce32382abdfb447a56531f86e225583fba08 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Wed, 26 Mar 2014 08:48:10 -0400 Subject: [PATCH 8/8] Convert incoming novice images to ubyte --- skimage/novice/_novice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index 6f0cb5de..553da070 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -254,7 +254,7 @@ class Picture(object): path = os.path.abspath(path) self._path = path with file_or_url_context(path) as context: - self.array = io.imread(context) + self.array = img_as_ubyte(io.imread(context)) self._format = imghdr.what(context) elif array is not None: self.array = array