Py学习  »  Python

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

SantoshGupta7 • 5 年前 • 1823 次点击  

我有一个编码为数据url的图像。

如何用python显示原始图像?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/57050
 
1823 次点击  
文章 [ 1 ]  |  最新文章 5 年前
Tom
Reply   •   1 楼
Tom    6 年前

可以使用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()