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
投稿

猜你喜欢

  • php打印输出棋盘的实现方法

    2023-10-09 04:38:10
  • python模块之re正则表达式详解

    2021-08-15 03:34:52
  • python实现连续变量最优分箱详解--CART算法

    2023-01-15 16:33:05
  • Python中函数及默认参数的定义与调用操作实例分析

    2022-01-15 05:16:31
  • 基于Python制作一个汇率换算程序

    2022-05-25 20:33:25
  • python如何爬取个性签名

    2021-03-29 03:34:04
  • Django模板之基本的 for 循环 和 List内容的显示方式

    2021-09-24 05:18:24
  • Python中super函数用法实例分析

    2023-12-05 14:36:19
  • 深入了解Python中Lambda函数的用法

    2023-02-03 10:09:01
  • Oracle DBA常用语句

    2009-08-05 20:15:00
  • 通过python下载FTP上的文件夹的实现代码

    2022-03-16 11:19:12
  • 带你了解Python妙开根号的三种方式

    2021-10-18 08:27:56
  • HTML编辑器FCKeditor使用详解

    2010-02-28 12:30:00
  • DIV+CSS高度自适应网页代码实例

    2008-09-20 08:00:00
  • 在Python程序员面试中被问的最多的10道题

    2022-02-27 08:20:03
  • python读取nc数据并绘图的方法实例

    2023-09-16 10:08:19
  • 教你精确编写高质量高性能的MySQL语法

    2009-01-14 12:57:00
  • 如何给 legend 标签设定宽度

    2008-07-26 12:18:00
  • PHP strip_tags() 去字符串中的 HTML、XML 以及 PHP 标签的函数

    2023-06-09 01:05:00
  • 记录PHP错误日志 display_errors与log_errors的区别

    2023-11-14 09:38:29
  • asp之家 网络编程 m.aspxhome.com