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 相关文章推荐
可用于监控 mysql Master Slave 状态的python代码
Feb 10 Python
Python文本相似性计算之编辑距离详解
Nov 28 Python
Python连接SQLServer2000的方法详解
Apr 19 Python
python并发2之使用asyncio处理并发
Dec 21 Python
浅析python打包工具distutils、setuptools
Apr 20 Python
django.db.utils.ProgrammingError: (1146, u“Table‘’ doesn’t exist”)问题的解决
Jul 13 Python
Python中的取模运算方法
Nov 10 Python
pandas实现DataFrame显示最大行列,不省略显示实例
Dec 26 Python
Python3运算符常见用法分析
Feb 14 Python
Python面向对象程序设计之静态方法、类方法、属性方法原理与用法分析
Mar 23 Python
利用Python判断你的密码难度等级
Jun 02 Python
教你用Python matplotlib库制作简单的动画
Jun 11 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/10/02 PHP
11个PHP 分页脚本推荐
2011/08/15 PHP
codeigniter教程之多文件上传使用示例
2014/02/11 PHP
ThinkPHP实现跨模块调用操作方法概述
2014/06/20 PHP
php实现基于openssl的加密解密方法
2016/09/30 PHP
PHP中Cookie的使用详解(简单易懂)
2017/04/28 PHP
PHP7扩展开发之hello word实现方法详解
2018/01/15 PHP
JS创建优美的页面滑动块效果 - Glider.js
2007/09/27 Javascript
Google排名中的10个最著名的 JavaScript库
2010/04/27 Javascript
AJAX异步从优酷专辑中采集所有视频及信息(JavaScript代码)
2010/11/20 Javascript
玩转jQuery按钮 请告诉我你最喜欢哪些?
2012/01/08 Javascript
JavaScript判断密码强度(自写代码)
2013/09/06 Javascript
JS关闭窗口或JS关闭页面的几种代码分享
2013/10/25 Javascript
ie浏览器使用js导出网页到excel并打印
2014/03/11 Javascript
node.js中的console.timeEnd方法使用说明
2014/12/09 Javascript
JQuery删除DOM节点的方法
2015/06/11 Javascript
jQuery旋转插件jqueryrotate用法详解
2016/10/13 Javascript
JavaScript 轮播图和自定义滚动条配合鼠标滚轮分享代码贴
2016/10/28 Javascript
利用python微信库itchat实现微信自动回复功能
2017/05/18 Python
python中的闭包函数
2018/02/09 Python
Django csrf 验证问题的实现
2018/10/09 Python
pytorch 转换矩阵的维数位置方法
2018/12/08 Python
关于Python中的向量相加和numpy中的向量相加效率对比
2019/08/26 Python
完美解决ARIMA模型中plot_acf画不出图的问题
2020/06/04 Python
Django QuerySet查询集原理及代码实例
2020/06/13 Python
Eyeko美国:屡获殊荣的睫毛膏、眼线笔和眉妆
2018/07/05 全球购物
总裁秘书岗位职责
2013/12/04 职场文书
简历中自我评价范文3则
2013/12/14 职场文书
数学国培研修感言
2014/02/13 职场文书
室内拓展活动方案
2014/02/13 职场文书
“六查”、“三学”、“三干”查摆问题整改措施
2014/09/27 职场文书
运动会开幕词
2015/01/28 职场文书
预备党员考察意见范文
2015/06/01 职场文书
Java数据结构之链表相关知识总结
2021/06/18 Java/Android
JMeter对MySQL数据库进行压力测试的实现步骤
2022/01/22 MySQL
Pandas实现批量拆分与合并Excel的示例代码
2022/05/30 Python