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自动化运维之IP地址处理模块详解
Dec 10 Python
python http接口自动化脚本详解
Jan 02 Python
使用Selenium破解新浪微博的四宫格验证码
Oct 19 Python
Python3.5 + sklearn利用SVM自动识别字母验证码方法示例
May 10 Python
Python OpenCV之图片缩放的实现(cv2.resize)
Jun 28 Python
简单了解python的break、continue、pass
Jul 08 Python
python制作朋友圈九宫格图片
Nov 03 Python
Python全局锁中如何合理运用多线程(多进程)
Nov 06 Python
Django实现列表页商品数据返回教程
Apr 03 Python
Python库skimage绘制二值图像代码实例
Apr 10 Python
Python使用matplotlib绘制圆形代码实例
May 27 Python
简单的Python人脸识别系统
Jul 14 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的超级变量$_GET获取HTML表单(Form) 数据
2011/05/07 PHP
基于PHP文件操作的详解
2013/06/05 PHP
PHP 记录访客的浏览信息方法
2018/01/29 PHP
基础的prototype.js常用函数及其用法
2007/03/10 Javascript
Juqery Html(),append()等方法的Bug解决方法
2010/12/13 Javascript
JavaScript快速检测浏览器对CSS3特性的支持情况
2012/09/26 Javascript
解析window.open的使用方法总结
2013/06/19 Javascript
使用JavaScript实现网页版Pongo设计思路及源代码分享
2014/06/16 Javascript
JS获取客户端IP地址、MAC和主机名的7个方法汇总
2014/07/21 Javascript
Jquery仿IGoogle实现可拖动窗口示例代码
2014/08/22 Javascript
2则自己编写的jQuery特效分享
2015/02/26 Javascript
javascript中call和apply的用法示例分析
2015/04/02 Javascript
jQuery 翻页组件yunm.pager.js实现div局部刷新的思路
2016/08/11 Javascript
js 转json格式的字符串为对象或数组(前后台)的方法
2016/11/02 Javascript
Vue实例中生命周期created和mounted的区别详解
2017/08/25 Javascript
JS去掉字符串中所有的逗号
2017/10/18 Javascript
详解webpack 入门与解析
2018/04/09 Javascript
用Node编写RESTful API接口的示例代码
2018/07/04 Javascript
VUE.CLI4.0配置多页面入口的实现
2019/11/25 Javascript
OpenLayer学习之自定义测量控件
2020/09/28 Javascript
[54:58]完美世界DOTA2联赛PWL S2 LBZS vs Rebirth 第一场 11.25
2020/11/25 DOTA
[57:31]DOTA2-DPC中国联赛 正赛 SAG vs CDEC BO3 第一场 2月1日
2021/03/11 DOTA
python字符串编码识别模块chardet简单应用
2015/06/15 Python
Python自动登录126邮箱的方法
2015/07/10 Python
django实现登录时候输入密码错误5次锁定用户十分钟
2017/11/05 Python
Scrapy抓取京东商品、豆瓣电影及代码分享
2017/11/23 Python
python删除过期log文件操作实例解析
2018/01/31 Python
基于Django与ajax之间的json传输方法
2018/05/29 Python
[原创]Python入门教程5. 字典基本操作【定义、运算、常用函数】
2018/11/01 Python
python 构造三维全零数组的方法
2018/11/12 Python
Django如何实现密码错误报错提醒
2020/09/04 Python
澳大利亚领先的武术用品和健身器材供应商:SMAI
2019/03/24 全球购物
VICHY薇姿俄罗斯官方网上商店:法国护肤品牌,火山温泉水
2019/11/22 全球购物
业务主管岗位职责范本
2013/12/25 职场文书
历史名人教你十五个读书方法,赶快Get起来!
2019/07/18 职场文书
CentOS7设置ssh服务以及端口修改方式
2022/12/24 Servers