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 2.7.x 和 3.x 版本的重要区别小结
Nov 28 Python
Python 和 JS 有哪些相同之处
Nov 23 Python
python3写爬取B站视频弹幕功能
Dec 22 Python
Python使用pymongo模块操作MongoDB的方法示例
Jul 20 Python
简单了解python中对象的取反运算符
Jul 01 Python
Django中的FBV和CBV用法详解
Sep 15 Python
Python可变对象与不可变对象原理解析
Feb 25 Python
Python类和实例的属性机制原理详解
Mar 21 Python
python中urllib.request和requests的使用及区别详解
May 05 Python
Python基于network模块制作电影人物关系图
Jun 19 Python
工程师必须了解的LRU缓存淘汰算法以及python实现过程
Oct 15 Python
详解Python3.8+PyQt5+pyqt5-tools+Pycharm配置详细教程
Nov 02 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
全文搜索和替换
2006/10/09 PHP
PHP获取当前页面完整URL的实现代码
2013/06/10 PHP
PHP代码实现爬虫记录――超管用
2015/07/31 PHP
Yii2――使用数据库操作汇总(增删查改、事务)
2016/12/19 PHP
ThinkPHP框架获取最后一次执行SQL语句及变量调试简单操作示例
2018/06/13 PHP
js 距离某一时间点时间是多少实现代码
2013/10/14 Javascript
javascript 原型链维护和继承详解
2014/11/26 Javascript
BootStrap智能表单实战系列(七)验证的支持
2016/06/13 Javascript
JS实现给对象动态添加属性的方法
2017/01/05 Javascript
微信小程序 setData的使用方法详解
2017/04/20 Javascript
jQuery UI实现动画效果代码分享
2018/08/19 jQuery
对vue中的事件穿透与禁止穿透实例详解
2019/10/28 Javascript
vue双向绑定数据限制长度的方法
2019/11/04 Javascript
解决vant的Toast组件时提示not defined的问题
2020/11/11 Javascript
python实现文件名批量替换和内容替换
2014/03/20 Python
Python实现对PPT文件进行截图操作的方法
2015/04/28 Python
Python的math模块中的常用数学函数整理
2016/02/04 Python
python使用Apriori算法进行关联性解析
2017/12/21 Python
Django渲染Markdown文章目录的方法示例
2019/01/02 Python
Python之虚拟环境virtualenv,pipreqs生成项目依赖第三方包的方法
2019/07/23 Python
Python 中的pygame安装与配置教程详解
2020/02/10 Python
HTML5 在canvas中绘制矩形附效果图
2014/06/23 HTML / CSS
香港太阳眼镜网上商店:SmartBuyGlasses香港
2016/07/22 全球购物
简约控的天堂:The Undone
2016/12/21 全球购物
西班牙三叶草药房:Farmacias Trébol
2019/05/03 全球购物
PHP开发的一般流程
2013/08/13 面试题
高一自我鉴定
2013/12/17 职场文书
个人现实表现材料
2014/02/04 职场文书
爱护公共设施标语
2014/06/24 职场文书
公司法人授权委托书范本
2014/09/12 职场文书
副总经理岗位职责
2015/02/02 职场文书
职称评定个人总结
2015/03/05 职场文书
2019年聘任书的写作格式及范文!
2019/07/03 职场文书
HashMap实现保存两个key相同的数据
2021/06/30 Java/Android
C站最全Python标准库总结,你想要的都在这里
2021/07/03 Python
Logback 使用TurboFilter实现日志级别等内容的动态修改操作
2021/08/30 Java/Android