【发布时间】:2019-12-10 11:48:32
【问题描述】:
正如标题所述,我正在尝试将 fig 转换为 PIL.Image。我目前能够通过首先将fig 保存到磁盘然后使用Image.open() 打开该文件来做到这一点,但是该过程花费的时间比预期的要长,我希望通过跳过本地保存步骤会更快一些.
这是我目前所拥有的:
# build fig
figsize, dpi = self._calc_fig_size_res(img_height)
fig = plt.Figure(figsize=figsize)
canvas = FigureCanvas(fig)
ax = fig.add_subplot(111)
ax.imshow(torch.from_numpy(S).flip(0), cmap = cmap)
fig.subplots_adjust(left = 0, right = 1, bottom = 0, top = 1)
ax.axis('tight'); ax.axis('off')
# export
fig.savefig(export_path, dpi = dpi)
# open image as PIL object
img = Image.open(export_path)
我在构建无花果后尝试过这样做(它会在导出阶段之前):
pil_img = Image.frombytes('RGB', canvas.get_width_height(), canvas.tostring_rgb())
但它没有显示整个图像。它看起来像是左上角的一部分,但它可能只是数据的一种奇怪表示——我正在使用频谱图,所以图像相当抽象。
【问题讨论】:
标签: python python-3.x numpy matplotlib python-imaging-library