Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Matplotlib imshow显示GIF图像_Python_Numpy_Matplotlib_Python Imaging Library - Fatal编程技术网

Python 使用Matplotlib imshow显示GIF图像

Python 使用Matplotlib imshow显示GIF图像,python,numpy,matplotlib,python-imaging-library,Python,Numpy,Matplotlib,Python Imaging Library,我需要使用ax.imshow()显示matplotlib绘图的背景。我将使用的背景图像是GIF图像。尽管安装了PIL,但以下代码仍会导致一个错误,抱怨未安装Python映像库(PIL): 使用PIL直接读取图像效果更好: from PIL import Image import numpy img = Image.open("test.gif") img_arr = asarray(img.getdata(), dtype=numpy.uint8) 但是,重塑阵列形状时,以下代码不起作用: i

我需要使用
ax.imshow()
显示matplotlib绘图的背景。我将使用的背景图像是GIF图像。尽管安装了PIL,但以下代码仍会导致一个错误,抱怨未安装Python映像库(PIL):

使用PIL直接读取图像效果更好:

from PIL import Image
import numpy
img = Image.open("test.gif")
img_arr = asarray(img.getdata(), dtype=numpy.uint8)
但是,重塑阵列形状时,以下代码不起作用:

img_arr = img_arr.reshape(img.size[0], img.size[1], 3) #Note the number 3
原因是实际颜色信息包含在通过
img.getcolors()
img.getpalette()
访问的颜色表中


使用
imread()
Image.open()
打开图像时,将所有图像转换为PNG或其他合适的格式,从而生成RGB图像,这不是一个选项。我可以使用PIL转换图像,但我认为解决方案很丑陋。因此,问题如下:是否有一种简单而快速(图像为5000 x 5000像素)的方法将GIF图像转换为RGB(在RAM中),以便我可以使用
imshow()
显示它们?

您需要先将GIF转换为RGB:

img = Image.open("test.gif").convert('RGB')
见这个问题:

img = Image.open("test.gif").convert('RGB')