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 Django(图文)
Nov 04 Python
Python基于twisted实现简单的web服务器
Sep 29 Python
Python自动化构建工具scons使用入门笔记
Mar 10 Python
在Python程序中操作文件之isatty()方法的使用教程
May 24 Python
python查看FTP是否能连接成功的方法
Jul 30 Python
Python之re操作方法(详解)
Jun 14 Python
python调用百度REST API实现语音识别
Aug 30 Python
Python3.4解释器用法简单示例
Mar 22 Python
Python单元测试工具doctest和unittest使用解析
Sep 02 Python
pytorch制作自己的LMDB数据操作示例
Dec 18 Python
信号生成及DFT的python实现方式
Feb 25 Python
Python爬虫之Selenium实现键盘事件
Dec 04 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 表单提交给自己
2008/07/24 PHP
PHP 截取字符串函数整理(支持gb2312和utf-8)
2010/02/16 PHP
ezSQL PHP数据库操作类库
2010/05/16 PHP
PHP substr 截取字符串出现乱码问题解决方法[utf8与gb2312]
2011/12/16 PHP
PHP正则匹配日期和时间(时间戳转换)的实例代码
2016/12/14 PHP
浅谈PHP中new self()和new static()的区别
2017/08/11 PHP
laravel通用化的CURD的实现
2019/12/13 PHP
jQuery学习7 操作JavaScript对象和集合的函数
2010/02/07 Javascript
基于jQuery的星级评分插件
2011/08/12 Javascript
JQuery 返回布尔值Is()条件判断方法代码
2012/05/14 Javascript
JS阻止冒泡事件以及默认事件发生的简单方法
2014/01/17 Javascript
jQuery控制cookie过期时间的方法
2015/04/07 Javascript
JavaScript如何获取数组最大值和最小值
2015/11/18 Javascript
javascript实现网站加入收藏功能
2015/12/16 Javascript
js放大镜放大购物图片效果
2017/01/18 Javascript
分享vue.js devtools遇到一系列问题
2017/10/24 Javascript
ActiveX控件的使用-js实现打印超市小票功能代码详解
2017/11/22 Javascript
详解mpvue开发小程序小总结
2018/07/25 Javascript
jQuery删除/清空指定元素的所有子节点实例代码
2019/07/04 jQuery
VUE DEMO之模拟登录个人中心页面之间数据传值实例
2019/10/31 Javascript
Python中用于去除空格的三个函数的使用小结
2015/04/07 Python
Python的Tornado框架实现异步非阻塞访问数据库的示例
2016/06/30 Python
Python打印输出数组中全部元素
2018/03/13 Python
通过python的matplotlib包将Tensorflow数据进行可视化的方法
2019/01/09 Python
python实现学员管理系统
2019/02/26 Python
解决python多行注释引发缩进错误的问题
2019/08/23 Python
python selenium实现发送带附件的邮件代码实例
2019/12/10 Python
Django调用支付宝接口代码实例详解
2020/04/04 Python
用pushplus+python监控亚马逊到货动态推送微信
2021/01/29 Python
HTML5 video 上传预览图片视频如何设置、预览视频某秒的海报帧
2018/08/28 HTML / CSS
全球最大最受欢迎的旅游社区:Tripadvisor
2017/11/03 全球购物
Super-Pharm波兰:药房和香水在一个地方
2020/08/18 全球购物
公立医院改革实施方案
2014/03/14 职场文书
转让协议书范本
2014/04/15 职场文书
雨中的树观后感
2015/06/03 职场文书
python基于tkinter制作无损音乐下载工具
2021/03/29 Python