python使用Tkinter显示网络图片的方法

yipeiwu_com6年前Python基础

本文实例讲述了python使用Tkinter显示网络图片的方法。分享给大家供大家参考。具体实现方法如下:

''' tk_image_view_url_io.py
display an image from a URL using Tkinter, PIL and data_stream
tested with Python27 and Python33 by vegaseat 01mar2013
'''
import io
# allows for image formats other than gif
from PIL import Image, ImageTk
try:
  # Python2
  import Tkinter as tk
  from urllib2 import urlopen
except ImportError:
  # Python3
  import tkinter as tk
  from urllib.request import urlopen
root = tk.Tk()
# find yourself a picture on an internet web page you like
# (right click on the picture, under properties copy the address)
#url = "/zb_users/upload/202003/v01ovqqs1eg.gif"
# or use image previously downloaded to tinypic.com
#url = "/zb_users/upload/202003/a5a2v0plvnk.jpg"
url = "/zb_users/upload/202003/d1oysqr5atw.jpg"
image_bytes = urlopen(url).read()
# internal data file
data_stream = io.BytesIO(image_bytes)
# open as a PIL image object
pil_image = Image.open(data_stream)
# optionally show image info
# get the size of the image
w, h = pil_image.size
# split off image file name
fname = url.split('/')[-1]
sf = "{} ({}x{})".format(fname, w, h)
root.title(sf)
# convert PIL image object to Tkinter PhotoImage object
tk_image = ImageTk.PhotoImage(pil_image)
# put the image on a typical widget
label = tk.Label(root, image=tk_image, bg='brown')
label.pack(padx=5, pady=5)
root.mainloop()

希望本文所述对大家的Python程序设计有所帮助。

相关文章

如何利用Python模拟GitHub登录详解

如何利用Python模拟GitHub登录详解

前言 最近学习了Fiddler抓包工具的简单使用,通过抓包,我们可以抓取到HTTP请求,并对其进行分析。现在我准备尝试着结合Python来模拟GitHub登录。 Fiddler抓包分析...

python使用urllib模块和pyquery实现阿里巴巴排名查询

urllib基础模块的应用,通过该类获取到url中的html文档信息,内部可以重写代理的获取方法 复制代码 代码如下:class ProxyScrapy(object): &n...

详解django+django-celery+celery的整合实战

详解django+django-celery+celery的整合实战

本篇文章主要是由于计划使用django写一个计划任务出来,可以定时的轮换值班人员名称或者定时执行脚本等功能,百度无数坑之后,终于可以凑合把这套东西部署上。本人英文不好,英文好或者希望深入...

django mysql数据库及图片上传接口详解

django mysql数据库及图片上传接口详解

前言 我们在 django-rest-framework解析请求参数 文章中完成了接口文档到参数解析, 一个完整的流程中还缺少对数据库的操作. 本篇内容为djang...

Python如何处理大数据?3个技巧效率提升攻略(推荐)

如果你有个5、6 G 大小的文件,想把文件内容读出来做一些处理然后存到另外的文件去,你会使用什么进行处理呢?不用在线等,给几个错误示范:有人用multiprocessing 处理,但是效...