【问题标题】:Python display a server-side image in a htmlPython 在 html 中显示服务器端图像
【发布时间】:2013-03-06 08:47:18
【问题描述】:

我在服务器文件夹中有一张图片。在处理 Get 请求的 do_GET() 函数中,我想发回图像。我选择使用 self.wfile.write('')。谁能告诉我如何将图像的来源包含在 img 标签中?还是有更好的方法来做到这一点?谢谢。

【问题讨论】:

  • 你在写一个网络服务器吗?如果没有,则使用网络服务器为您的应用程序提供静态资源。

标签: python html directory image


【解决方案1】:

您可以使用这样的数据 URI 在 img 标签中包含图像的“来源”:

<img alt="Embedded Image" src="data:image/png;base64,<your base64 encoding here>" />

使用base64 python标准库生成base64字符串:

import base64

with open("image.png", "rb") as image:
    encoded_string = base64.b64encode(image.read())

【讨论】:

    猜你喜欢
    • 2019-09-30
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 2014-02-07
    • 2014-01-16
    • 1970-01-01
    • 2012-12-24
    • 2021-03-07
    相关资源
    最近更新 更多