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


Posted in Python onApril 24, 2015

本文实例讲述了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 = "http://www.google.com/intl/en/images/logo.gif"
# or use image previously downloaded to tinypic.com
#url = "http://i48.tinypic.com/w6sjn6.jpg"
url = "http://i50.tinypic.com/34g8vo5.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 相关文章推荐
Python的Flask框架中实现分页功能的教程
Apr 20 Python
python删除指定类型(或非指定)的文件实例详解
Jul 06 Python
Python使用ntplib库同步校准当地时间的方法
Jul 02 Python
python中的字典使用分享
Jul 31 Python
Python中的日期时间处理详解
Nov 17 Python
python广度优先搜索得到两点间最短路径
Jan 17 Python
python实现感知器算法(批处理)
Jan 18 Python
解决使用PyCharm时无法启动控制台的问题
Jan 19 Python
Django接收自定义http header过程详解
Aug 23 Python
pymysql模块的使用(增删改查)详解
Sep 09 Python
使用Python的turtle模块画国旗
Sep 24 Python
详解pycharm自动import所需的库的操作方法
Nov 30 Python
Python中最常用的操作列表的几种方法归纳
Apr 24 #Python
在Python中使用lambda高效操作列表的教程
Apr 24 #Python
使用Python的判断语句模拟三目运算
Apr 24 #Python
Python的字典和列表的使用中一些需要注意的地方
Apr 24 #Python
整理Python最基本的操作字典的方法
Apr 24 #Python
编写Python脚本使得web页面上的代码高亮显示
Apr 24 #Python
Python UnicodeEncodeError: 'gbk' codec can't encode character 解决方法
Apr 24 #Python
You might like
十天学会php之第三天
2006/10/09 PHP
针对初学PHP者的疑难问答(1)
2006/10/09 PHP
FCKeditor添加自定义按钮
2008/03/27 PHP
PHP中限制IP段访问、禁止IP提交表单的代码
2011/04/23 PHP
PHP If Else(elsefi) 语句
2013/04/07 PHP
php轻松实现中英文混排字符串截取
2014/05/28 PHP
使用PHP+MySql+Ajax+jQuery实现省市区三级联动功能示例
2017/09/15 PHP
PHP扩展Swoole实现实时异步任务队列示例
2019/04/13 PHP
PHP高并发和大流量解决方案整理
2019/12/24 PHP
thinkphp框架表单数组实现图片批量上传功能示例
2020/04/04 PHP
js 实现复制到粘贴板的功能代码
2010/05/13 Javascript
javascript解决IE6下hover问题的方法
2015/07/28 Javascript
js实现拉幕效果的广告代码
2015/09/02 Javascript
项目实践一图片上传之form表单还是base64前端图片压缩(前端图片压缩)
2016/07/28 Javascript
Bootstrap和Java分页实例第一篇
2016/12/23 Javascript
vue左右侧联动滚动的实现代码
2018/06/06 Javascript
JS数组中对象去重操作示例
2019/06/04 Javascript
Vue+iview+webpack ie浏览器兼容简单处理
2019/09/20 Javascript
vue-router 按需加载 component: () => import() 报错的解决
2020/09/22 Javascript
[01:18:45]DOTA2-DPC中国联赛 正赛 DLG vs Dragon BO3 第三场2月1日
2021/03/11 DOTA
Python的净值数据接口调用示例分享
2016/03/15 Python
Ubuntu 下 vim 搭建python 环境 配置
2017/06/12 Python
CentOS下使用yum安装python-pip失败的完美解决方法
2017/08/16 Python
Python解决八皇后问题示例
2018/04/22 Python
django组合搜索实现过程详解(附代码)
2019/08/06 Python
Pyinstaller 打包exe教程及问题解决
2019/08/16 Python
浅析python redis的连接及相关操作
2019/11/07 Python
python中@contextmanager实例用法
2021/02/07 Python
关于VPN
2012/06/10 面试题
外贸业务员求职自荐信分享
2013/09/21 职场文书
家长会演讲稿
2014/04/26 职场文书
辩护词格式
2015/05/22 职场文书
小数乘法教学反思
2016/02/22 职场文书
2019班干部竞选演讲稿范本!
2019/07/08 职场文书
导游词之丹东鸭绿江
2019/10/24 职场文书
Python实现滑雪小游戏
2021/09/25 Python