-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
BUG Raise exception for invalid input #2632
Conversation
Arrays of non-uint8 types were assumed to be in 0..1 range. When this was not true and integer values were used, only the low-order bits were used, resulting in a mangled image. Now, an explicit exception is raised. Closes issue matplotlib#2499
@@ -248,6 +248,8 @@ def to_rgba(self, x, alpha=None, bytes=False): | |||
xx = x | |||
else: | |||
raise ValueError("third dimension must be 3 or 4") | |||
if xx.dtype != np.uint8 and xx.max() > 1: | |||
raise ValueError("image must be either uint8 or in the 0..1 range") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test failure is a pep8 violation on this line.
Sorry, I didn't look at this change closely enough last night. The docstring on |
I don't think we want to be doing this 0-1 check at all. What would make sense is to raise an exception if the input is integer type but not uint8, since uint8 is the only supported integer type. This check is fast to do, and entirely consistent with the logic of the method; it would require a change to the docstring to remove the statement that dtype is not checked. It would have to be clear that the integer-is-uint8 check is done only in the rgb or rgba case. In the mappable array case, any integer type is allowed, triggering direct indexing into the colormap LUT. |
Right now matplotlib correctly displays binary images. As a user, I'd prefer if it kept that functionality. |
@luispedro Would you elaborate, please, on your last comment? I don't understand what it means with respect to this PR, and my preceding recommendation. What do you mean by "displays binary images"? With what kinds of input array? |
I meant boolean images. For example:
I feel that at least |
Included in #6122. |
Arrays of non-uint8 types were assumed to be in 0..1 range.
When this was not true and integer values were used, only the low-order
bits were used, resulting in a mangled image. Now, an explicit exception
is raised.
Closes issue #2499