diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index 03ec85f3..806e814f 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -9,6 +9,8 @@ except ImportError: "Please refer to http://pypi.python.org/pypi/PIL/ " "for further instructions.") +from skimage.util import img_as_ubyte + def imread(fname, dtype=None): """Load an image from file. @@ -106,7 +108,4 @@ def imshow(arr): [0, 1]. Images of dtype uint8 are in [0, 255]. """ - if np.issubdtype(arr.dtype, float): - arr = (arr * 255).astype(np.uint8) - - Image.fromarray(arr).show() + Image.fromarray(img_as_ubyte(arr)).show() diff --git a/skimage/io/_plugins/util.py b/skimage/io/_plugins/util.py index 2f37074c..1bf2c89f 100644 --- a/skimage/io/_plugins/util.py +++ b/skimage/io/_plugins/util.py @@ -2,6 +2,7 @@ import numpy as np from . import _colormixer from . import _histograms import threading +from skimage.util import img_as_ubyte # utilities to make life easier for plugin writers. @@ -132,25 +133,18 @@ def prepare_for_display(npy_img): width = npy_img.shape[1] out = np.empty((height, width, 3), dtype=np.uint8) + npy_img = img_as_ubyte(npy_img) if npy_img.ndim == 2 or \ (npy_img.ndim == 3 and npy_img.shape[2] == 1): npy_plane = npy_img.reshape((height, width)) - if np.issubdtype(npy_img.dtype, float): - out[:,:,0] = npy_plane*255 - out[:,:,1] = out[:,:,0] - out[:,:,2] = out[:,:,0] - else: - out[:,:,0] = npy_plane - out[:,:,1] = npy_plane - out[:,:,2] = npy_plane + out[:,:,0] = npy_plane + out[:,:,1] = npy_plane + out[:,:,2] = npy_plane elif npy_img.ndim == 3: if npy_img.shape[2] == 3 or npy_img.shape[2] == 4: - if np.issubdtype(npy_img.dtype, float): - out[:,:,:3] = (npy_img[:,:,:3])*255 - else: - out[:,:,:3] = npy_img[:,:,:3] + out[:,:,:3] = npy_img[:,:,:3] else: raise ValueError('Image must have 1, 3, or 4 channels')