且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从 matplotlib 图像中获取 RGBA 数组

更新时间:2023-01-27 12:14:22

您可以使用 as_rgba_str 从图像中获取图像数据.举个例子:

You can use as_rgba_str to get the image data from the image. Here's an example:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2, 1000)
X, Y = np.meshgrid(x, x)
data = np.sin((X-2)**3 + Y**4)

im = plt.imshow(data)

x = im.make_image()
h, w, d = x.as_rgba_str()
n = np.fromstring(d, dtype=np.uint8).reshape(h, w, 4)

plt.figure()
plt.imshow(n[:,:,0], cmap="gray", origin='lower')

plt.show()

原图:

RGBA 数据中的 R 通道(注意所有蓝色部分都是黑色的,因为它们中没有红色):

The R channel from the RGBA data (note all the blue sections are black since these have no red in them):