Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mode incorrectly determined on Image.fromarray with bool input dtype #3109

Closed
akhmerov opened this issue Apr 21, 2018 · 9 comments · Fixed by #3790
Closed

Mode incorrectly determined on Image.fromarray with bool input dtype #3109

akhmerov opened this issue Apr 21, 2018 · 9 comments · Fixed by #3790
Labels
Anaconda Issues with Anaconda's Pillow Bug Any unexpected behavior, until confirmed feature. NumPy

Comments

@akhmerov
Copy link

What did you do?

I observed that Image.fromarray works incorrectly on bool array input, likely due to an incorrectly determined mode or rawmode.

import PIL
import numpy as np
print(np.array(PIL.Image.fromarray(np.eye(5, dtype=bool))))

What did you expect to happen?

Same as print(np.eye(5, dtype=bool)), so:

[[ True False False False False]
 [False  True False False False]
 [False False  True False False]
 [False False False  True False]
 [False False False False  True]]

What actually happened?

[[ True False False False False]
 [False False False False False]
 [False False False False  True]
 [False False False  True False]
 [False False  True False False]]

What versions of Pillow and Python are you using?

Pillow: 5.1.0
python: '3.6.5 | packaged by conda-forge | (default, Apr 6 2018, 13:39:56) \n[GCC 4.8.2 20140120 (Red Hat 4.8.2-15)]'
numpy: 1.14.2

Workaround

Use Image.fromarray(boolean_array * np.uint(255))

@aclark4life
Copy link
Member

@akhmerov How about a PR to address this issue?

@aclark4life aclark4life added Bug Any unexpected behavior, until confirmed feature. NumPy and removed NumPy labels Jun 30, 2018
@akhmerov
Copy link
Author

akhmerov commented Jul 1, 2018

@aclark4life sorry, I'm not familiar enough with the codebase and especially with the low level numpy format to identify the source of the issue and fix it efficiently. (I did check if I could come up with an easy fix when trying to figure out what is happening.)

@wiredfool
Copy link
Member

Probably related to: #2044 and #350

@radarhere
Copy link
Member

I've created PR #3790 to resolve this.

@nico-zck
Copy link

Bad news for guys, this bug still exists:

print(np.array(Image.fromarray(np.eye(5, dtype=bool))))
[[ True False False False False]
 [False False False False False]
 [False False False False  True]
 [False False False  True False]
 [False False  True False False]]

Even worse, it will crash when you set argument mode='1' for fromarray function:

print(np.array(Image.fromarray(np.eye(5, dtype=bool), mode='1')))
[[False False False False False]
 [False False False False False]
 [False False False False False]
 [False False False False False]
 [False False False False False]]

Related issues:
#350
#1228
#1727
Oops, this bug has a long history since 2013.

My environment:
python:3.7.3
Pillow: 6.0
numpy: 1.16.3

@hugovk
Copy link
Member

hugovk commented May 23, 2019

@Zhangchenkai The fix is not in Pillow 6.0.0 (released ~2019-04-01), it will be in Pillow 6.1.0 due for release on 2019-07-01.

Please retest with master or wait for the release.

@stranskj
Copy link

stranskj commented Apr 6, 2020

The bug is still in releases on Pypi. Pillow 7.1.1, Python 3.8.2

@akhmerov
Copy link
Author

akhmerov commented Apr 6, 2020

@stranskj can't confirm with the same versions of Pillow and Python.

EDIT: just in case, numpy version 1.18.1, and I have a conda-forge + linux installation.

@hugovk
Copy link
Member

hugovk commented Apr 6, 2020

Also cannot reproduce (both installed from PyPI via pip):

Python 3.8.2 (v3.8.2:7b3ab5921f, Feb 24 2020, 17:52:18)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(.startup.py)
(imported collections, datetime, itertools, math, os, pprint, re, sys, time)
>>> from PIL import Image
>>> import numpy as np
>>> Image.__version__
'7.1.1'
>>> np.__version__
'1.18.2'
>>> print(np.array(Image.fromarray(np.eye(5, dtype=bool))))
[[ True False False False False]
 [False  True False False False]
 [False False  True False False]
 [False False False  True False]
 [False False False False  True]]
>>> print(np.eye(5, dtype=bool))
[[ True False False False False]
 [False  True False False False]
 [False False  True False False]
 [False False False  True False]
 [False False False False  True]]
>>> np.array(Image.fromarray(np.eye(5, dtype=bool))) == np.eye(5, dtype=bool)
array([[ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True],
       [ True,  True,  True,  True,  True]])

@stranskj If you still have a problem, please open a new issue and fill in the template with more details. Thanks.

https://github.com/python-pillow/Pillow/issues/new/choose

@aclark4life aclark4life added the Anaconda Issues with Anaconda's Pillow label May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Anaconda Issues with Anaconda's Pillow Bug Any unexpected behavior, until confirmed feature. NumPy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants