【问题标题】:IPython.core.display.HTML to pngIPython.core.display.HTML 到 png
【发布时间】:2022-01-25 01:34:32
【问题描述】:

关注这个话题 Adding image to pandas DataFrame 我使用path_to_image_html 在我的 df pandas 数据框中显示一些 png(实际上是 matplotlib 中的图表)。

def toto():
   df['imageUrls'] = images1
   def path_to_image_html(path):
       return '<img src="'+ path + '" width="60" >'
   return HTML(df.to_html(escape=False,formatters=dict(imageUrls=path_to_image_html)))

然后display(toto()) 在我的 jupyter 笔记本中显示我的熊猫。

现在我想以 PNG 文件格式保存我的熊猫。 基本上将 toto() 转换为 png 文件。

我该如何继续?谢谢

【问题讨论】:

    标签: python html pandas png


    【解决方案1】:

    您可以使用:imgkit

    import pandas as pd
    import imgkit
    
    test = pd.DataFrame({"id": [1,2,3,4], 
                         "percentage": [0.2, 0.4, 0.5, 0.6]})
    
    imgkit.from_string(test.to_html(), 'out.png')
    

    【讨论】:

    • 是的,我的 toto() 函数应该直接返回 df.to_html(escape=False,formatters=dict(imageUrls=path_to_image_html)) Thk !
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2013-03-09
    • 2012-07-09
    • 1970-01-01
    相关资源
    最近更新 更多