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判断质数(素数)的简单方法讲解
May 05 Python
python 类详解及简单实例
Mar 24 Python
pandas全表查询定位某个值所在行列的方法
Apr 12 Python
python删除本地夹里重复文件的方法
Nov 19 Python
selenium+python自动化测试之鼠标和键盘事件
Jan 23 Python
python使用pygame模块实现坦克大战游戏
Mar 25 Python
python异常触发及自定义异常类解析
Aug 06 Python
解决Python列表字符不区分大小写的问题
Dec 19 Python
快速解决jupyter启动卡死的问题
Apr 10 Python
python怎么提高计算速度
Jun 11 Python
python主要用于哪些方向
Jul 05 Python
python批量修改文件名的示例
Sep 27 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获取当前文件所在目录 getcwd()函数
2009/05/13 PHP
PHP记录和读取JSON格式日志文件
2016/07/07 PHP
前台js改变Session的值(用ajax实现)
2012/12/28 Javascript
js和css写一个可以自动隐藏的悬浮框
2014/03/05 Javascript
jquery.hotkeys监听键盘按下事件keydown插件
2014/05/11 Javascript
使用jQuery管理选择结果
2015/01/20 Javascript
jQuery简单实现仿京东商城的左侧菜单效果代码
2015/09/09 Javascript
JavaScript类型系统之正则表达式
2016/01/05 Javascript
Node.js下自定义错误类型详解
2016/10/17 Javascript
使用JavaScript根据图片获取条形码的方法
2017/07/04 Javascript
jQuery实现的页面详情展开收起功能示例
2018/06/11 jQuery
三分钟学会用ES7中的Async/Await进行异步编程
2018/06/14 Javascript
Webpack按需加载打包chunk命名的方法
2019/09/22 Javascript
JavaScript中的null和undefined用法解析
2019/09/30 Javascript
jquery实现异步文件上传ajaxfileupload.js
2020/10/23 jQuery
[01:21]DOTA2新纪元-7.0新版本即将开启!
2016/12/11 DOTA
python一键去抖音视频水印工具
2018/09/14 Python
python使用turtle绘制国际象棋棋盘
2019/05/23 Python
python实现复制大量文件功能
2019/08/31 Python
python模拟预测一下新型冠状病毒肺炎的数据
2020/02/01 Python
pyecharts动态轨迹图的实现示例
2020/04/17 Python
使用CSS3实现多列布局与多背景的技巧
2016/02/29 HTML / CSS
美国知名珠宝首饰品牌:Gemvara
2017/10/06 全球购物
为奢侈时尚带来了慈善元素:Olivela
2018/09/29 全球购物
如何用JQuery进行表单验证
2013/05/29 面试题
办公室前台的岗位职责
2013/12/20 职场文书
促销活动策划方案
2014/01/12 职场文书
大学生秋游活动方案
2014/02/17 职场文书
集中采购方案
2014/06/10 职场文书
安全资料员岗位职责范本
2014/06/28 职场文书
2014年人力资源部工作总结
2014/11/19 职场文书
资料员岗位职责
2015/02/10 职场文书
商场圣诞节活动总结
2015/05/06 职场文书
如何将numpy二维数组中的np.nan值替换为指定的值
2021/05/14 Python
JavaWeb 入门篇:创建Web项目,Idea配置tomcat
2021/07/16 Java/Android
windows系统安装配置nginx环境
2022/06/28 Servers