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')