简体   繁体   English

将pyplot图转换成wand.image图片

[英]Convert pyplot figure into wand.image Image

Is there a way to convert a pyplot figure created with pyplot.Figure into a wand image?有没有办法将使用 pyplot.Figure 创建的pyplot.Figure图转换为魔杖图像? I have tried using the following to no avail:我尝试使用以下无济于事:

image_data = BytesIO()
figure.savefig(image_data, format='png')
image_data.seek(0)
image = Image(file=image_data, resolution=250)

The end goal of this is to convert a list of figures into a long png.这样做的最终目标是将图形列表转换为长 png。 The only other method (which is ugly) is to convert to pdf and then concatenate the pages.唯一的其他方法(很难看)是转换为 pdf,然后连接页面。

I believe you are on the right track. 我相信您的方向正确。 Without seeing the figure, I would assume the issue would be related to holding the C structure pointer using the with keyword. 如果没有看到该图,我将认为问题与使用with关键字持有C结构指针的有关。

image_data = BytesIO()
figure.savefig(image_data, dpi=250, format='png')
image_data.seek(0)
with Image(file=image_data) as img:
    # ... do work
    img.save(filename='/tmp/out.png')

I was trying to figure out how to do this same thing. 我试图弄清楚该怎么做。 I went down a rabbit hole for a bit thinking I needed to also use PIL (Pillow) to accomplish this task. 我掉进一个兔子洞有点想我也需要使用PIL(枕头)来完成此任务。 With the help of the previous answer I was able to come up with a complete example: 在上一个答案的帮助下,我得以提出一个完整的例子:

import matplotlib
from io import BytesIO
import numpy
import matplotlib.pyplot as plt
from wand.display import display
from wand.image import Image

plt.plot([1,5,3,2])
plt.ylabel('y axis numbers')
plt.xlabel('x axis numbers')

image_data = BytesIO() #Create empty in-memory file
plt.savefig(image_data, format='png') #Save pyplot figure to in-memory file
image_data.seek(0) #Move stream position back to beginning of file 
img = Image(file=image_data) #Create wand.image
display(img) #Use wand to display the img

I tried the recommended code above and had no luck.我尝试了上面推荐的代码,但没有成功。 I posted the question to the WandB forum ( here ) and the following was recommended:我将问题发布到 WandB 论坛(此处),并推荐了以下内容:

fig, ax1 = plt.subplots(...)
...
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
wandb.log(({"chart": wandb.Image(Image.open(buf)) }))
fig.show()

It seems that using the file parameter is no longer allowed.似乎不再允许使用file参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM