簡體   English   中英

將pyplot圖轉換成wand.image圖片

[英]Convert pyplot figure into wand.image Image

有沒有辦法將使用 pyplot.Figure 創建的pyplot.Figure圖轉換為魔杖圖像? 我嘗試使用以下無濟於事:

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

這樣做的最終目標是將圖形列表轉換為長 png。 唯一的其他方法(很難看)是轉換為 pdf,然后連接頁面。

我相信您的方向正確。 如果沒有看到該圖,我將認為問題與使用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')

我試圖弄清楚該怎么做。 我掉進一個兔子洞有點想我也需要使用PIL(枕頭)來完成此任務。 在上一個答案的幫助下,我得以提出一個完整的例子:

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

我嘗試了上面推薦的代碼,但沒有成功。 我將問題發布到 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()

似乎不再允許使用file參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM