image - 从 rgb 数组创建 matplotlib 图
问题描述
我有一个 png 图像,我想将其制作成 matplotlib 图。我可以通过执行以下操作将 png 图像转换为 matplotlib.image.AxesImage 对象(尽管可能有更好的方法):
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
def rgba2rgb(rgba, background=(255,255,255)):
row, col, ch = rgba.shape
if ch == 3:
return rgba
assert ch == 4, 'RGBA image has 4 channels.'
rgb = np.zeros( (row, col, 3), dtype='float32' )
r, g, b, a = rgba[:,:,0], rgba[:,:,1], rgba[:,:,2], rgba[:,:,3]
a = np.asarray( a, dtype='float32' )/255
R, G, B = background
rgb[:,:,0] = r * a + (1.0 - a) * R
rgb[:,:,1] = g * a + (1.0 - a) * G
rgb[:,:,2] = b * a + (1.0 - a) * B
return np.asarray( rgb, dtype='uint8' )
img = Image.open('image.png')
rgba_arr = np.array(img)
rgb_array = rgba2rgb(rgba_arr)
plt_img = plt.imshow(rgb_array, aspect='auto')
同样,我遇到的主要问题是 plt.imshow 的结果是 matplotlib.image.AxesImage 而不是 matplotlib 图。我怎样才能使这个数字?谢谢。
解决方案
推荐阅读
- java - 实例化类型参数和覆盖方法
- c++ - 如何在 Visual Studio 2017 中将 Ncurses 添加到我的项目中?
- npm - 重命名 angular 文件夹后,出现错误:“您似乎不依赖于“@angular/core”。这是一个错误”
- r - 如何在 R-Markdown 文件中折叠打印输出?
- r - plot_ly() 中缺少的 y 轴数
- google-drive-api - 在 Google Docs 中打开文档,无法转换文档
- r - R:从数据框列表中选择列,而某些列在少数数据框中不存在
- asp.net-core-2.0 - ASP.NET CORE 2.2 调用针对 .net 框架的 Web api 不会被调用
- php - 使用 WooCommerce 订单项获取特定数据结构
- python-3.x - 如何将多个数字转换为 ASCII?