mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-19 11:27:45 +08:00
Merge pull request #1375 from tonysyu/fix/pil-compatibility
Fix compatibility with PIL
This commit is contained in:
@@ -176,20 +176,23 @@ def ndarray_to_pil(arr, format_str=None):
|
||||
mode = 'L'
|
||||
mode_base = 'L'
|
||||
|
||||
try:
|
||||
array_buffer = arr.tobytes()
|
||||
except AttributeError:
|
||||
array_buffer = arr.tostring() # Numpy < 1.9
|
||||
|
||||
if arr.ndim == 2:
|
||||
im = Image.new(mode_base, arr.T.shape)
|
||||
try:
|
||||
im.frombytes(arr.tobytes(), 'raw', mode)
|
||||
im.frombytes(array_buffer, 'raw', mode)
|
||||
except AttributeError:
|
||||
im.frombytes(arr.tostring(), 'raw', mode)
|
||||
|
||||
im.fromstring(array_buffer, 'raw', mode) # PIL 1.1.7
|
||||
else:
|
||||
image_shape = (arr.shape[1], arr.shape[0])
|
||||
try:
|
||||
im = Image.frombytes(mode, (arr.shape[1], arr.shape[0]),
|
||||
arr.tobytes())
|
||||
im = Image.frombytes(mode, image_shape, array_buffer)
|
||||
except AttributeError:
|
||||
im = Image.frombytes(mode, (arr.shape[1], arr.shape[0]),
|
||||
arr.tostring())
|
||||
im = Image.fromstring(mode, image_shape, array_buffer) # PIL 1.1.7
|
||||
return im
|
||||
|
||||
|
||||
|
||||
@@ -195,7 +195,10 @@ def test_cmyk():
|
||||
fname = f.name
|
||||
f.close()
|
||||
img.save(fname)
|
||||
img.close()
|
||||
try:
|
||||
img.close()
|
||||
except AttributeError: # `close` not available on PIL
|
||||
pass
|
||||
|
||||
new = imread(fname)
|
||||
|
||||
@@ -231,4 +234,4 @@ class TestSaveTIF:
|
||||
yield self.roundtrip, dtype, x
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
||||
run_module_suite()
|
||||
|
||||
Reference in New Issue
Block a user