python使用Tkinter显示网络图片的方法
作者:feiwen 时间:2021-09-26 18:25:38
本文实例讲述了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,网络,图片
0
投稿
猜你喜欢
从0到1搭建后端架构的演进(MVC,服务拆分,微服务,领域驱动)
2022-04-24 10:03:35
Python3爬虫学习之应对网站反爬虫机制的方法分析
2022-04-17 08:44:16
SQL Server数据库和Oracle行转列的特殊方案描述
2010-07-26 15:14:00
ASP.NET操作MySql数据库的实例代码讲解
2024-01-17 15:00:01
OpenCV学习方框滤波实现图像处理代码示例
2023-02-08 17:17:43
如何实现优惠打折?
2010-06-03 10:27:00
Windows server 2008 r2上安装MySQL5.7.10步骤
2024-01-15 06:47:11
如何利用python读取图片属性信息
2023-12-24 00:57:51
Python中垃圾回收和del语句详解
2023-12-20 01:02:55
CentOS6.9 Python环境配置(python2.7、pip、virtualenv)
2022-04-30 14:37:08
Pyqt5 实现跳转界面并关闭当前界面的方法
2023-02-02 13:59:19
mysql中数据库覆盖导入的几种方式总结
2024-01-19 22:26:33
关于Python正则表达式模块之re模块
2022-12-31 08:03:46
python使用MQTT给硬件传输图片的实现方法
2022-02-09 21:55:59
Python用 matplotlib 绘制柱状图
2023-01-22 01:20:50
在Qt中正确的设置窗体的背景图片的几种方法总结
2023-05-11 11:15:01
Python代码覆盖率统计工具coverage.py用法详解
2021-02-02 22:55:51
MSSQL数据加密解密代码
2023-07-08 18:45:30
JS+CSS实现闪烁字体效果代码
2024-04-18 09:31:04
Python操作MySQL数据库的方法
2024-01-28 10:49:27