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实现的简单万年历例子分享
Apr 25 Python
用Python输出一个杨辉三角的例子
Jun 13 Python
Python实现的检测web服务器健康状况的小程序
Sep 17 Python
python创建和删除目录的方法
Apr 29 Python
Python的净值数据接口调用示例分享
Mar 15 Python
用Python写一个无界面的2048小游戏
May 24 Python
Python PyQt5实现的简易计算器功能示例
Aug 23 Python
Python简单计算数组元素平均值的方法示例
Dec 26 Python
pytorch 固定部分参数训练的方法
Aug 17 Python
flask框架自定义url转换器操作详解
Jan 25 Python
django models里数据表插入数据id自增操作
Jul 15 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
VB中的RasEnumConnections函数返回632错误解决方法
2014/07/29 PHP
destoon实现不同会员组公司名称显示不同的颜色的方法
2014/08/22 PHP
php自定义urlencode,urldecode函数实例
2015/03/24 PHP
Mac环境下php操作mysql数据库的方法分享
2015/05/11 PHP
图文详解PHP环境搭建教程
2016/07/16 PHP
document对象execCommand的command参数介绍
2006/08/01 Javascript
js获取变量
2006/08/24 Javascript
javascript-简单的日历实现及Date对象语法介绍(附图)
2013/05/30 Javascript
jquery清空表单数据示例分享
2014/02/13 Javascript
Javascript判断文件是否存在(客户端/服务器端)
2014/09/16 Javascript
JavaScript日期类型的一些用法介绍
2015/03/02 Javascript
javascript检测flash插件是否被禁用的方法
2016/01/14 Javascript
学习Javascript面向对象编程之封装
2016/02/23 Javascript
利用纯Vue.js构建Bootstrap组件
2016/11/03 Javascript
详解前端自动化工具gulp自动添加版本号
2016/12/20 Javascript
angularJS+requireJS实现controller及directive的按需加载示例
2017/02/20 Javascript
基于VUE实现的九宫格抽奖功能
2018/09/30 Javascript
通过图带你深入了解vue的响应式原理
2019/06/21 Javascript
layuiAdmin循环遍历展示商品图片列表的方法
2019/09/16 Javascript
Python操作Redis之设置key的过期时间实例代码
2018/01/25 Python
在Python中构建增广矩阵的实现方法
2019/07/01 Python
将pymysql获取到的数据类型是tuple转化为pandas方式
2020/05/15 Python
Python如何安装第三方模块
2020/05/28 Python
sklearn和keras的数据切分与交叉验证的实例详解
2020/06/19 Python
Python如何使用ElementTree解析xml
2020/10/12 Python
纯HTML+CSS3制作导航菜单(附源码)
2013/04/24 HTML / CSS
一款基于css3和jquery实现的动画显示弹出层按钮教程
2015/01/04 HTML / CSS
在网上学习全世界最好的课程:Coursera
2017/11/07 全球购物
俄罗斯大型在线书店:Читай-город
2019/10/10 全球购物
工厂厂长岗位职责
2013/11/08 职场文书
生物技术专业毕业生求职信范文
2013/12/14 职场文书
解除劳动合同协议书(样本)
2014/10/02 职场文书
工作违纪检讨书范文
2015/01/26 职场文书
胡雪岩故居导游词
2015/02/06 职场文书
《云雀的心愿》教学反思
2016/02/23 职场文书
零基础学java之循环语句的使用
2022/04/10 Java/Android