Repository navigation
Closed
Labels
Milestone
Description
Hi
I was using python3.5.2, numpy 1.14.1, and matplotlib 2.1.2 and encountered a problem when I attempted to show a picture with pyplot. It always gave errors saying 'numpy.ndarray object has no attribute mask', and the related source code seemed in 'matplotlib/image.py'.
I'm not sure whether it was caused by matplotlib or numpy. How do I fix this ?
$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>> from PIL import Image
>>> img=Image.open('test.jpg')
>>> imgfig = plt.figure()
>>> imgplt = imgfig.add_subplot(111)
>>> imgplt.imshow(img)
<matplotlib.image.AxesImage object at 0x7ff142877438>
>>> plt.show()
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
return self.func(*args)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 233, in resize
self.show()
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 304, in draw
FigureCanvasAgg.draw(self)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 430, in draw
self.figure.draw(self.renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py", line 1299, in draw
renderer, self, artists, self.suppressComposite)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/axes/_base.py", line 2437, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 566, in draw
renderer, renderer.get_image_magnification())
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 793, in make_image
unsampled=unsampled)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 430, in _make_image
if A.mask.shape == A.shape:
AttributeError: 'numpy.ndarray' object has no attribute 'mask'
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
return self.func(*args)
File "/usr/lib/python3.5/tkinter/__init__.py", line 599, in callit
func(*args)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 323, in idle_draw
self.draw()
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_tkagg.py", line 304, in draw
FigureCanvasAgg.draw(self)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/backends/backend_agg.py", line 430, in draw
self.figure.draw(self.renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/figure.py", line 1299, in draw
renderer, self, artists, self.suppressComposite)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/axes/_base.py", line 2437, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 138, in _draw_list_compositing_images
a.draw(renderer)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 566, in draw
renderer, renderer.get_image_magnification())
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 793, in make_image
unsampled=unsampled)
File "/usr/local/lib/python3.5/dist-packages/matplotlib/image.py", line 430, in _make_image
if A.mask.shape == A.shape:
AttributeError: 'numpy.ndarray' object has no attribute 'mask'
Activity
[-]get error 'numpy.ndarray' object has no attribute 'mask' when show a picture(image)[/-][+]get error when show a picture(image) : 'numpy.ndarray' object has no attribute 'mask'[/+]efiring commentedon Feb 27, 2018
This looks like a bug that is still in master, based on code inspection.
pil_to_arrayis returning a plain ndarray, but everything else going into the image data attribute is converted to a masked array, and subsequent code assumes that at least 2-D arrays are masked. The quickest fix would be for the output of pil_to_array to be converted to a masked array in theset_datamethod. Otherwise, one would have to look carefully at all possible code paths and ensure that they handle ndarrays and masked arrays equally.jklymak commentedon Feb 27, 2018
OK, I'm almost in exactly this code block in #10613. I'll add to that PR if thats OK....
jklymak commentedon Feb 27, 2018
OK, I can't reproduce this on master.
works fine.
NewThinker-Jeffrey commentedon Feb 27, 2018
Hi @jklymak
Have you tried this code on branch v2.1.2 ? And do you get the same errors with me? If not, the problem may be caused by myself and I'll handle it later (it's not urgent to me and I don't have time to dig into it today).
I don't want to waste your time because of my own mistake.
efiring commentedon Feb 27, 2018
I wonder whether it depends on the type of jpg file. @NewThinker-Jiwey can you supply the example you used? I think that the jpg can be returned as a 2-D array or a 3-D array, and the problem would be with the former. Again, this is just based on looking fairly quickly at the code.
jklymak commentedon Feb 27, 2018
Yeah, its also possible my pillow and numpy are a bit old... I'll check soon
jklymak commentedon Feb 27, 2018
numpy1.14 andmatplotlib2.1.2 I can't get this to trigger on a generic jpg.NewThinker-Jeffrey commentedon Feb 27, 2018
@efiring Yeah. The picture is here :
https://github.com/NewThinker-Jiwey/DabblingInPython/blob/pic/examples/test.jpg
efiring commentedon Feb 27, 2018
With that file I can reproduce it. It is a monochrome file, so it is coming through as a 2-D array and going through the color-mapping code path. That's where it is being assumed to be a masked array.
efiring commentedon Feb 27, 2018
The quick fix would be here (in image.py with line numbers):
changing line 617 to
Untested, and it might not be the best way to fix it.
NewThinker-Jeffrey commentedon Feb 27, 2018
@efiring Got it, thanks !!
For workaround I can show the pic by convert it to RGB beforehand:
jklymak commentedon Feb 27, 2018
@efiring - that will indeed allow the image to be plotted, but it will be coloured w/ the default colormap (and linear norm), rather than displayed as greyscale, which I assume most folks who use PIL would want?
I've fixed in #10613 as:
I didn't change
pil_to_arraysince its technically correct to convert to a 2-D array, but it just doesn't work w/imshow.efiring commentedon Feb 27, 2018
Thanks, that makes perfect sense.
jpambrun commentedon Mar 1, 2018
I can work around the issue by converting the PIL image to a numpy array (e.g.
plt.imshow(np.array(im1), cmap="gray")).The proposed fix will show up correctly, but the pixel probe will show the four RGBA values instead of the usual single intensity value. Also, saving the figure may result in the wrong format being used?
jklymak commentedon Mar 2, 2018
@jpambrun Sure, you can also display the image that way. But matplotlib will still save the resulting image as RGBA, so I don't think converting to RGBA at this step hurts anything, and saves the user from having to supply a colormap.
tacaswell commentedon Mar 3, 2018
Closed by #10613
Bug Fix: pyplot.imshow error
fix: Force convert output of images_square_grid to RGB