From 6b908c1bb18d3a7eb04e2fa6e979176c32eff9e0 Mon Sep 17 00:00:00 2001 From: Warren Weckesser Date: Wed, 18 Nov 2015 13:55:57 -0500 Subject: [PATCH 1/2] BUG: io: imread in the PIL plugin drops indexed PNG alpha channel. When using the PIL plugin to read an indexed PNG file that has an alpha channel, the alpha channel would be lost. --- skimage/data/foo3x5x4indexed.png | Bin 0 -> 116 bytes skimage/io/_plugins/pil_plugin.py | 5 ++++- skimage/io/tests/test_pil.py | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 skimage/data/foo3x5x4indexed.png diff --git a/skimage/data/foo3x5x4indexed.png b/skimage/data/foo3x5x4indexed.png new file mode 100644 index 0000000000000000000000000000000000000000..cc969d03dd263fde0293045012129ad1e01ea824 GIT binary patch literal 116 zcmeAS@N?(olHy`uVBq!ia0vp^tU%1n!3-pGUYMQ&Qak}ZA+8Jz{~6@}*E9S-aNz$2 z)p@}{ah8%GzhH*{{~6}#uLo%o_H=O!shE?Tz{JRwmd2)#z{1GD@Qp!iqy9%JpfrQ0 LtDnm{r-UW|Gdmrm literal 0 HcmV?d00001 diff --git a/skimage/io/_plugins/pil_plugin.py b/skimage/io/_plugins/pil_plugin.py index 7cb8e5c1..59811dd2 100644 --- a/skimage/io/_plugins/pil_plugin.py +++ b/skimage/io/_plugins/pil_plugin.py @@ -85,7 +85,10 @@ def pil_to_ndarray(im, dtype=None, img_num=None): if grayscale: frame = im.convert('L') else: - frame = im.convert('RGB') + if im.format == 'PNG' and 'transparency' in im.info: + frame = im.convert('RGBA') + else: + frame = im.convert('RGB') elif im.mode == '1': frame = im.convert('L') diff --git a/skimage/io/tests/test_pil.py b/skimage/io/tests/test_pil.py index ad3870a6..ee962906 100644 --- a/skimage/io/tests/test_pil.py +++ b/skimage/io/tests/test_pil.py @@ -84,6 +84,28 @@ def test_imread_palette(): assert img.ndim == 3 +def test_imread_index_png_with_alpha(): + # The file `foo3x5x4indexed.png` was created with this array + # (3x5 is (height)x(width)): + data = np.array([[[127, 0, 255, 255], + [127, 0, 255, 255], + [127, 0, 255, 255], + [127, 0, 255, 255], + [127, 0, 255, 255]], + [[192, 192, 255, 0], + [192, 192, 255, 0], + [0, 0, 255, 0], + [0, 0, 255, 0], + [0, 0, 255, 0]], + [[0, 31, 255, 255], + [0, 31, 255, 255], + [0, 31, 255, 255], + [0, 31, 255, 255], + [0, 31, 255, 255]]], dtype=np.uint8) + img = imread(os.path.join(data_dir, 'foo3x5x4indexed.png')) + assert_array_equal(img, data) + + def test_palette_is_gray(): gray = Image.open(os.path.join(data_dir, 'palette_gray.png')) assert _palette_is_grayscale(gray) From 3251d3c07a8eecee0ac0fc84b82d290d3f250b50 Mon Sep 17 00:00:00 2001 From: Warren Weckesser Date: Mon, 7 Dec 2015 01:47:35 -0500 Subject: [PATCH 2/2] BLD: Increase the minimum pillow version to 2.1.0. --- requirements.txt | 2 +- tools/travis_before_install.sh | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/requirements.txt b/requirements.txt index 8b97dd4b..19c541f5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,5 +3,5 @@ numpy>=1.6.1 scipy>=0.9.0 six>=1.4 networkx>=1.8 -pillow>=1.7.8 +pillow>=2.1.0 dask[array]>=0.5.0 diff --git a/tools/travis_before_install.sh b/tools/travis_before_install.sh index 159fc2d3..8f131102 100755 --- a/tools/travis_before_install.sh +++ b/tools/travis_before_install.sh @@ -40,9 +40,6 @@ fi # test minimum requirements on 2.7 if [[ $TRAVIS_PYTHON_VERSION == 2.7* ]]; then sed -i 's/>=/==/g' requirements.txt - # PIL instead of Pillow - sed -i 's/pillow.*/pil==1.1.7/g' requirements.txt - WHEELBINARIES=${WHEELBINARIES/pillow/pil} fi # create new empty venv