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提取html文件中的特定数据的实现代码
Mar 24 Python
python实现人人网登录示例分享
Jan 19 Python
Python中处理字符串之isalpha()方法的使用
May 18 Python
Python自动化测试Eclipse+Pydev 搭建开发环境
Aug 15 Python
详解Python核心编程中的浅拷贝与深拷贝
Jan 07 Python
python2.7和NLTK安装详细教程
Sep 19 Python
Python列表删除元素del、pop()和remove()的区别小结
Sep 11 Python
python安装scipy的步骤解析
Sep 28 Python
Django 实现对已存在的model进行更改
Mar 28 Python
Django REST framwork的权限验证实例
Apr 02 Python
OpenCV4.1.0+VS2017环境配置的方法步骤
Jul 09 Python
利用python对mysql表做全局模糊搜索并分页实例
Jul 12 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(1)
2006/10/09 PHP
摘自织梦CMS中的图片处理类
2015/08/08 PHP
详细解读PHP的Yii框架中登陆功能的实现
2015/08/21 PHP
PHP编程开发怎么提高编程效率 提高PHP编程技术
2015/11/09 PHP
浅谈ThinkPHP中initialize和construct的区别
2017/04/01 PHP
Javascript将string类型转换int类型
2010/12/09 Javascript
javascript学习笔记(十八) 获得页面中的元素代码
2012/06/20 Javascript
document.documentElement的一些使用技巧
2013/04/18 Javascript
什么是 AngularJS?AngularJS简介
2014/12/06 Javascript
JavaScript获取Url里的参数
2014/12/18 Javascript
Javascript中获取对象的原型对象的方法小结
2015/02/25 Javascript
深入理解(function(){... })();
2016/08/16 Javascript
JS实现页面中所有img对象添加onclick事件及新窗口查看图片的方法
2016/12/27 Javascript
详解webpack 热更新优化
2018/09/13 Javascript
解决VUE项目使用Element-ui 下拉组件的验证失效问题
2020/11/07 Javascript
webpack4从0搭建组件库的实现
2020/11/29 Javascript
python数据结构之二叉树的建立实例
2014/04/29 Python
python网络编程学习笔记(二):socket建立网络客户端
2014/06/09 Python
python算法演练_One Rule 算法(详解)
2017/05/17 Python
python把数组中的数字每行打印3个并保存在文档中的方法
2018/07/17 Python
Python格式化输出字符串方法小结【%与format】
2018/10/29 Python
详解Python3 基本数据类型
2019/04/19 Python
Python可迭代对象操作示例
2019/05/07 Python
jupyter notebook 中输出pyecharts图实例
2020/04/23 Python
解决Pycharm 导入其他文件夹源码的2种方法
2020/02/12 Python
用python给csv里的数据排序的具体代码
2020/07/17 Python
python打开音乐文件的实例方法
2020/07/21 Python
Django 用户认证Auth组件的使用
2020/11/30 Python
印度第一网上礼品店:IGP.com
2020/02/06 全球购物
学生就业推荐信
2013/11/13 职场文书
护理学毕业生求职信
2013/11/14 职场文书
校园文化标语
2014/06/18 职场文书
主题党日活动总结
2014/07/08 职场文书
大学生思想道德自我评价
2015/03/09 职场文书
2019年共青团工作条例最新版
2019/11/12 职场文书
python计算列表元素与乘积详情
2022/08/05 Python