欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

201121-将numpy数组等像素保存为图片

程序员文章站 2022-03-18 21:37:40
...

Specifying and saving a figure with exact size in pixels

# %% Demo
import matplotlib.pyplot as plt
from matplotlib.image import imread
import numpy

# Setting
w = 5
h = 5
im_np = numpy.random.rand(h, w)

# Plot and save
fig = plt.figure(frameon=False)
fig.set_size_inches(w,h)
ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
ax.imshow(im_np, aspect='auto',cmap=plt.cm.jet)
fig.savefig('figure.png', dpi=1)

# Load and show
im = imread("figure.png")
plt.imshow(im[:,:,3])
相关标签: matplot numpy