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模拟登录并且保持cookie的方法详解
Apr 04 Python
python+pyqt实现右下角弹出框
Oct 26 Python
python中实现k-means聚类算法详解
Nov 11 Python
pandas.DataFrame 根据条件新建列并赋值的方法
Apr 08 Python
python requests.post带head和body的实例
Jan 02 Python
Python设计模式之迭代器模式原理与用法实例分析
Jan 10 Python
python分割一个文本为多个文本的方法
Jul 22 Python
在python中用url_for构造URL的方法
Jul 25 Python
Python paramiko模块使用解析(实现ssh)
Aug 30 Python
Python使用type动态创建类操作示例
Feb 29 Python
python3读取autocad图形文件.py实例
Jun 05 Python
PyQt5通过信号实现MVC的示例
Feb 06 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中文件上传的一个问题
2010/09/04 PHP
php防止伪造的数据从URL提交方法
2014/06/27 PHP
javascript Array.remove() 数组删除
2009/08/06 Javascript
Javascript继承机制的设计思想分享
2011/08/28 Javascript
js保留两位小数使用toFixed实现
2013/07/29 Javascript
javascript获取所有同类checkbox选项(实例代码)
2013/11/07 Javascript
JavaScript实现的日期控件具体代码
2013/11/18 Javascript
在javascript中执行任意html代码的方法示例解读
2013/12/25 Javascript
jQuery 计算iframe 窗口大小的方法
2014/05/13 Javascript
jQuery+CSS3实现树叶飘落特效
2015/02/01 Javascript
javascript跨域原因以及解决方案分享
2015/04/08 Javascript
JavaScript中的anchor()方法使用详解
2015/06/08 Javascript
7个jQuery最佳实践
2016/01/12 Javascript
jquery插件bootstrapValidator数据验证详解
2016/11/09 Javascript
js仿京东轮播效果 选项卡套选项卡使用
2017/01/12 Javascript
谈谈JavaScript数组常用方法总结
2017/01/24 Javascript
整理关于Bootstrap导航的慕课笔记
2017/03/29 Javascript
Javascript Promise用法详解
2018/05/10 Javascript
小程序实现左滑删除功能
2018/10/30 Javascript
vue组件中传值EventBus的使用及注意事项说明
2020/11/16 Javascript
centos系统升级python 2.7.3
2014/07/03 Python
python实现简易内存监控
2018/06/21 Python
PyGame贪吃蛇的实现代码示例
2018/11/21 Python
在python 不同时区之间的差值与转换方法
2019/01/14 Python
Python爬虫图片懒加载技术 selenium和PhantomJS解析
2019/09/18 Python
pytorch中nn.Conv1d的用法详解
2019/12/31 Python
Spy++的使用方法及下载教程
2021/01/29 Python
英国最大的体育&时尚零售公司:JD Sports
2017/12/13 全球购物
Crocs波兰官方商店:女鞋、男鞋、童鞋、洞洞鞋
2019/10/08 全球购物
美国婴儿服装购物网站:Gerber Childrenswear
2020/05/06 全球购物
超级搞笑检讨书
2014/01/15 职场文书
餐厅考勤管理制度
2014/01/28 职场文书
预备党员自我评价范文
2015/03/04 职场文书
公司行政主管岗位职责
2015/04/09 职场文书
长江七号观后感
2015/06/11 职场文书
Spring Boot 整合 Apache Dubbo的示例代码
2021/07/04 Java/Android