matplotlib获取显示的图片二进制流与转ndarray


代码

import io
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure("Data", frameon=False)
plt.imshow(i, cmap='gray')
canvas = fig.canvas
plt.axis('off')
# 设置画布大小,单位英寸,1英寸=100px
fig.set_size_inches(272 / 100, 256 / 100)
plt.gca().xaxis.set_major_locator(plt.NullLocator())  # plt.gca()表示获取当前子图"Get Current Axes"。
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0)
plt.margins(0, 0)
buffer = io.BytesIO()
canvas.print_png(buffer)
data = buffer.getvalue()
buffer.write(data)
image = np.asarray(Image.open(buffer))

声明:HEUE NOTE|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA 4.0协议进行授权

转载:转载请注明原文链接 - matplotlib获取显示的图片二进制流与转ndarray