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程序设计有所帮助。

相关文章

zbar解码二维码和条形码示例

复制代码 代码如下:#!/usr/bin/env python# coding: u8import osimport zbarimport Imageimport urllibimpor...

python opencv如何实现图片绘制

python opencv如何实现图片绘制

这篇文章主要介绍了python opencv如何实现图片绘制,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 点和圆 : circle...

Django发送邮件功能实例详解

以126邮箱为例 1 首先进126邮箱设置,开启: √POP3/SMTP服务  √IMAP/SMTP服务 成功开启后会获得一个授权码。 2. setting.py配置:...

在Python程序员面试中被问的最多的10道题

我们在为大家整Python程序员面试试题中,发现了一些被面试官问到的最多的一些问题,以下就是本篇内容: Python是个非常受欢迎的编程语言,随着近些年机器学习、云计算等技术的发展,Py...

Python实现socket非阻塞通讯功能示例

本文实例讲述了Python实现socket非阻塞通讯功能。分享给大家供大家参考,具体如下: 非阻塞需要多线程编程 服务端 方式1: 使用threading库实现多线程 基本方法和单进程基...