如何在python中显示和显示数据url中的图像?

2024-04-20 14:30:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个编码为数据url的图像。你知道吗

如何在python中显示来自此的原始图像?你知道吗


Tags: 数据图像url编码
1条回答
网友
1楼 · 发布于 2024-04-20 14:30:21

您可以使用Tkinter打开一个用于查看图像的窗口,使用urllib读取图像数据,例如

import io
import base64
try:
    import Tkinter as tk
    from urllib2 import urlopen
except ImportError:
    import tkinter as tk
    from urllib.request import urlopen

root = tk.Tk()

image_url = "data:image/png;base64,iVB........"
image_byt = urlopen(image_url).read()
image_b64 = base64.encodestring(image_byt)

photo = tk.PhotoImage(data=image_b64)

cv = tk.Canvas(bg='white')
cv.pack(side='top', fill='both', expand='yes')

cv.create_image(10, 10, image=photo, anchor='nw')
root.mainloop()

相关问题 更多 >