python使用PIL缩放网络图片并保存的方法


Posted in Python onApril 24, 2015

本文实例讲述了python使用PIL缩放网络图片并保存的方法。分享给大家供大家参考。具体实现方法如下:

''' tk_image_view_url_io_resize.py
display an image from a URL using Tkinter, PIL and data_stream
also resize the web image to fit a certain size display widget
retaining its aspect ratio
Pil facilitates resizing and allows file formats other then gif
tested with Python27 and Python33 by vegaseat 18mar2013
'''
import io
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
def resize(w, h, w_box, h_box, pil_image):
  '''
  resize a pil_image object so it will fit into
  a box of size w_box times h_box, but retain aspect ratio
  '''
  f1 = 1.0*w_box/w # 1.0 forces float division in Python2
  f2 = 1.0*h_box/h
  factor = min([f1, f2])
  #print(f1, f2, factor) # test
  # use best down-sizing filter
  width = int(w*factor)
  height = int(h*factor)
  return pil_image.resize((width, height), Image.ANTIALIAS)
root = tk.Tk()
# size of image display box you want
w_box = 400
h_box = 350
# find yourself a picture on an internet web page you like
# (right click on the picture, under properties copy the address)
# a larger (1600 x 1200) picture from the internet
# url name is long, so split it
url1 = "http://freeflowerpictures.net/image/flowers/petunia/"
url2 = "petunia-flower.jpg"
url = url1 + url2
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)
# get the size of the image
w, h = pil_image.size
# resize the image so it retains its aspect ration
# but fits into the specified display box
pil_image_resized = resize(w, h, w_box, h_box, pil_image)
# optionally show resized image info ...
# get the size of the resized image
wr, hr = pil_image_resized.size
# split off image file name
fname = url.split('/')[-1]
sf = "resized {} ({}x{})".format(fname, wr, hr)
root.title(sf)
# convert PIL image object to Tkinter PhotoImage object
tk_image = ImageTk.PhotoImage(pil_image_resized)
# put the image on a widget the size of the specified display box
label = tk.Label(root, image=tk_image, width=w_box, height=h_box)
label.pack(padx=5, pady=5)
root.mainloop()

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

Python 相关文章推荐
python循环监控远程端口的方法
Mar 14 Python
在Python中使用lambda高效操作列表的教程
Apr 24 Python
Python遍历目录中的所有文件的方法
Jul 08 Python
Python实现的多项式拟合功能示例【基于matplotlib】
May 15 Python
说说如何遍历Python列表的方法示例
Feb 11 Python
Python实现微信自动好友验证,自动回复,发送群聊链接方法
Feb 21 Python
python实现AES加密与解密
Mar 28 Python
Django中reverse反转并且传递参数的方法
Aug 06 Python
如何关掉pycharm中的python console(图解)
Oct 31 Python
python3中numpy函数tile的用法详解
Dec 04 Python
python3连接kafka模块pykafka生产者简单封装代码
Dec 23 Python
详解tf.device()指定tensorflow运行的GPU或CPU设备实现
Feb 20 Python
python使用Tkinter显示网络图片的方法
Apr 24 #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
You might like
如何在PHP中使用Oracle数据库(6)
2006/10/09 PHP
PHP define函数的使用说明
2008/08/27 PHP
PHP创建XML的方法示例【基于DOMDocument类及SimpleXMLElement类】
2019/09/10 PHP
Laravel 修改验证异常的响应格式实例代码详解
2020/05/25 PHP
为jquery.ui.dialog 增加“自动记住关闭时的位置”的功能
2009/11/24 Javascript
jQuery ajax(复习)—Baidu ajax request分离版
2013/01/24 Javascript
javascript实现复制与粘贴操作实例
2014/10/16 Javascript
jQuery支持动态参数将函数绑定到事件上的方法
2015/03/17 Javascript
JavaScript使用DeviceOne开发实战(三)仿微信应用
2015/12/02 Javascript
JS 动态加载js文件和css文件 同步/异步的两种简单方式
2016/09/23 Javascript
JS闭包与延迟求值用法示例
2016/12/22 Javascript
jQuery代码实现实时获取时间
2017/01/29 Javascript
vue 2.0项目中如何引入element-ui详解
2017/09/06 Javascript
vue2中引用及使用 better-scroll的方法详解
2018/11/15 Javascript
详解Vue用cmd创建项目
2019/02/12 Javascript
微信小程序的注册页面包含倒计时验证码、获取用户信息
2019/05/22 Javascript
Vue图片浏览组件v-viewer用法分析【支持旋转、缩放、翻转等操作】
2019/11/04 Javascript
jQuery实现计算器功能
2020/10/19 jQuery
对于Python的框架中一些会话程序的管理
2015/04/20 Python
python实现下载指定网址所有图片的方法
2015/08/08 Python
python基础知识小结之集合
2015/11/25 Python
python实现最小二乘法线性拟合
2019/07/19 Python
Python进阶之迭代器与迭代器切片教程
2020/01/29 Python
Python jieba库分词模式实例用法
2021/01/13 Python
今天学到的CSS最新技术(与图片背景相关)
2012/12/24 HTML / CSS
HTML5 文件域+FileReader 分段读取文件并上传到服务器
2017/10/23 HTML / CSS
美国顶级奢侈茶:Mighty Leaf Tea(美泰茶)
2016/11/26 全球购物
运动服饰每月订阅盒:Ellie
2018/04/29 全球购物
Eton丹麦官网:精美的男式衬衫
2020/05/27 全球购物
大学生简单自荐信
2013/11/10 职场文书
普师专业个人自荐信范文
2013/11/26 职场文书
企业法人代表任命书
2014/06/06 职场文书
网络工程专业大学生求职信
2014/10/01 职场文书
公司慰问信范文
2015/03/23 职场文书
会议室管理制度范本
2015/08/06 职场文书
如何将numpy二维数组中的np.nan值替换为指定的值
2021/05/14 Python