mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-12 11:41:20 +08:00
Clean up tests and add to bento.info.
Remove "no plugin" test because we will always have a plugin. Fix handling of bools in ubyte check Fix integration of tifffile.c Clean up new tests. Fix _tifffile import and add to bento.info Add tifffile.py and fix pil_plugin import Add tifffile tests to test_pil and remove PIL import checks
This commit is contained in:
@@ -157,6 +157,9 @@ Library:
|
||||
Extension: skimage.graph._ncut_cy
|
||||
Sources:
|
||||
skimage/graph/_ncut_cy.pyx
|
||||
Extension: skimage.io._plugins._tifffile
|
||||
Sources:
|
||||
skimage/io/_plugins/tifffile.c
|
||||
|
||||
Executable: skivi
|
||||
Module: skimage.scripts.skivi
|
||||
|
||||
@@ -14,7 +14,7 @@ except ImportError:
|
||||
from skimage.util import img_as_ubyte, img_as_uint, img_as_int
|
||||
|
||||
from six import string_types
|
||||
from tifffile import imread as tif_imread, imsave as tif_imsave
|
||||
from .tifffile import imread as tif_imread, imsave as tif_imsave
|
||||
|
||||
|
||||
def imread(fname, dtype=None):
|
||||
@@ -29,13 +29,9 @@ def imread(fname, dtype=None):
|
||||
|
||||
|
||||
"""
|
||||
if hasattr(fname, 'name'):
|
||||
name = fname.name.lower()
|
||||
else:
|
||||
name = fname.lower()
|
||||
|
||||
if name.endswith(('.tiff', '.tif')) and dtype is None:
|
||||
return tif_imread(fname)
|
||||
if hasattr(fname, 'lower') and dtype is None:
|
||||
if fname.lower().endswith(('.tiff', '.tif')):
|
||||
return tif_imread(fname)
|
||||
|
||||
im = Image.open(fname)
|
||||
try:
|
||||
@@ -185,12 +181,15 @@ def imsave(fname, arr, format_str=None):
|
||||
if arr.dtype.kind == 'b':
|
||||
arr = arr.astype(np.uint8)
|
||||
|
||||
if hasattr(fname, 'name'):
|
||||
name = fname.name.lower()
|
||||
else:
|
||||
name = fname.lower()
|
||||
use_tif = False
|
||||
if hasattr(fname, 'lower'):
|
||||
if fname.lower().endswith(('.tiff', '.tif')):
|
||||
use_tif = True
|
||||
if not format_str is None:
|
||||
if format_str.lower() in ['tiff', 'tif']:
|
||||
use_tif = True
|
||||
|
||||
if name.endswith(('.tiff', '.tif')):
|
||||
if use_tif:
|
||||
tif_imsave(fname, arr)
|
||||
|
||||
else:
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -29,17 +29,5 @@ def test_imread_url():
|
||||
assert image.shape == (512, 512)
|
||||
|
||||
|
||||
@raises(RuntimeError)
|
||||
def test_imread_no_plugin():
|
||||
# tweak data path so that file URI works on both unix and windows.
|
||||
image_path = os.path.join(data_dir, 'lena.png')
|
||||
plugins = plugin_store['imread']
|
||||
plugin_store['imread'] = []
|
||||
try:
|
||||
io.imread(image_path)
|
||||
finally:
|
||||
plugin_store['imread'] = plugins
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
||||
|
||||
@@ -8,19 +8,13 @@ from tempfile import NamedTemporaryFile
|
||||
from skimage import data_dir
|
||||
from skimage.io import (imread, imsave, use_plugin, reset_plugins,
|
||||
Image as ioImage)
|
||||
from skimage.io.test.utils import ubyte_check, full_range_check
|
||||
from skimage.io.tests.utils import ubyte_check, full_range_check
|
||||
|
||||
from six import BytesIO
|
||||
|
||||
|
||||
try:
|
||||
from PIL import Image
|
||||
from skimage.io._plugins.pil_plugin import pil_to_ndarray, ndarray_to_pil, _palette_is_grayscale
|
||||
use_plugin('pil')
|
||||
except ImportError:
|
||||
PIL_available = False
|
||||
else:
|
||||
PIL_available = True
|
||||
from PIL import Image
|
||||
from skimage.io._plugins.pil_plugin import pil_to_ndarray, ndarray_to_pil, _palette_is_grayscale
|
||||
use_plugin('pil')
|
||||
|
||||
np.random.seed(0)
|
||||
|
||||
@@ -40,8 +34,6 @@ def setup_module(self):
|
||||
pass
|
||||
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_imread_flatten():
|
||||
# a color image is flattened
|
||||
img = imread(os.path.join(data_dir, 'color.png'), flatten=True)
|
||||
@@ -52,7 +44,6 @@ def test_imread_flatten():
|
||||
assert np.sctype2char(img.dtype) in np.typecodes['AllInteger']
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_imread_palette():
|
||||
img = imread(os.path.join(data_dir, 'palette_gray.png'))
|
||||
assert img.ndim == 2
|
||||
@@ -60,7 +51,6 @@ def test_imread_palette():
|
||||
assert img.ndim == 3
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_palette_is_gray():
|
||||
gray = Image.open(os.path.join(data_dir, 'palette_gray.png'))
|
||||
assert _palette_is_grayscale(gray)
|
||||
@@ -68,7 +58,6 @@ def test_palette_is_gray():
|
||||
assert not _palette_is_grayscale(color)
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_bilevel():
|
||||
expected = np.zeros((10, 10))
|
||||
expected[::2] = 255
|
||||
@@ -77,7 +66,6 @@ def test_bilevel():
|
||||
assert_array_equal(img, expected)
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_imread_uint16():
|
||||
expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy'))
|
||||
img = imread(os.path.join(data_dir, 'chessboard_GRAY_U16.tif'))
|
||||
@@ -85,7 +73,6 @@ def test_imread_uint16():
|
||||
assert_array_almost_equal(img, expected)
|
||||
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_repr_png():
|
||||
img_path = os.path.join(data_dir, 'camera.png')
|
||||
original_img = ioImage(imread(img_path))
|
||||
@@ -98,14 +85,12 @@ def test_repr_png():
|
||||
|
||||
assert np.all(original_img == round_trip)
|
||||
|
||||
@skipif(not PIL_available)
|
||||
|
||||
def test_imread_truncated_jpg():
|
||||
assert_raises((IOError, ValueError), imread,
|
||||
os.path.join(data_dir, 'truncated.jpg'))
|
||||
|
||||
# Big endian images not correctly loaded for PIL < 1.1.7
|
||||
# Renable test when PIL 1.1.7 is more common.
|
||||
@skipif(True)
|
||||
|
||||
def test_imread_uint16_big_endian():
|
||||
expected = np.load(os.path.join(data_dir, 'chessboard_GRAY_U8.npy'))
|
||||
img = imread(os.path.join(data_dir, 'chessboard_GRAY_U16B.tif'))
|
||||
@@ -141,11 +126,9 @@ class TestSave:
|
||||
x = (x * 255).astype(dtype)
|
||||
yield self.verify_roundtrip, dtype, x, roundtrip_function(x)
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_imsave_roundtrip_file(self):
|
||||
self.verify_imsave_roundtrip(self.roundtrip_file)
|
||||
|
||||
@skipif(not PIL_available)
|
||||
def test_imsave_roundtrip_pil_image(self):
|
||||
self.verify_imsave_roundtrip(self.roundtrip_pil_image)
|
||||
|
||||
@@ -175,17 +158,35 @@ def test_imexport_imimport():
|
||||
assert out.shape == shape
|
||||
|
||||
|
||||
@skip(not PIL_available)
|
||||
def test_all_color(self):
|
||||
@skipif(not PIL_available)
|
||||
def test_all_color():
|
||||
ubyte_check('pil')
|
||||
ubyte_check('pil', 'bmp')
|
||||
|
||||
|
||||
@skip(not PIL_available)
|
||||
def test_all_mono(self):
|
||||
@skipif(not PIL_available)
|
||||
def test_all_mono():
|
||||
full_range_check('pil')
|
||||
full_range_check('pil', 'tiff')
|
||||
|
||||
|
||||
class TestSaveTIF:
|
||||
def roundtrip(self, dtype, x):
|
||||
f = NamedTemporaryFile(suffix='.tif')
|
||||
fname = f.name
|
||||
f.close()
|
||||
sio.imsave(fname, x)
|
||||
y = sio.imread(fname)
|
||||
assert_array_equal(x, y)
|
||||
|
||||
def test_imsave_roundtrip(self):
|
||||
for shape in [(10, 10), (10, 10, 3), (10, 10, 4)]:
|
||||
for dtype in (np.uint8, np.uint16, np.float32, np.float64):
|
||||
x = np.ones(shape, dtype=dtype) * np.random.rand(*shape)
|
||||
|
||||
if not np.issubdtype(dtype, float):
|
||||
x = (x * 255).astype(dtype)
|
||||
yield self.roundtrip, dtype, x
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
||||
|
||||
@@ -29,7 +29,7 @@ def ubyte_check(plugin, fmt='png'):
|
||||
|
||||
img2 = img > 128
|
||||
r2 = roundtrip(img2, plugin, fmt)
|
||||
testing.assert_allclose(img2, img_as_bool(r2))
|
||||
testing.assert_allclose(img2.astype(np.uint8), r2)
|
||||
|
||||
img3 = img_as_float(img)
|
||||
r3 = roundtrip(img3, plugin, fmt)
|
||||
|
||||
Reference in New Issue
Block a user