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实现实例
Apr 26 Python
python爬虫入门教程之糗百图片爬虫代码分享
Sep 02 Python
python中实现定制类的特殊方法总结
Sep 28 Python
使用Python的Scrapy框架编写web爬虫的简单示例
Apr 17 Python
python多进程和多线程究竟谁更快(详解)
May 29 Python
tensorflow 加载部分变量的实例讲解
Jul 27 Python
Python函数返回不定数量的值方法
Jan 22 Python
详解Python连接MySQL数据库的多种方式
Apr 16 Python
python 梯度法求解函数极值的实例
Jul 10 Python
python、Matlab求定积分的实现
Nov 20 Python
Python类反射机制使用实例解析
Dec 30 Python
详解Python描述符的工作原理
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
怎么使 Mysql 数据同步
2006/10/09 PHP
Mysql的常用命令
2006/10/09 PHP
第四章 php数学运算
2011/12/30 PHP
ajax取消挂起请求的处理方法
2013/03/18 PHP
一个严格的PHP Session会话超时时间设置方法
2014/06/10 PHP
php中使用session_set_save_handler()函数把session保存到MySQL数据库实例
2014/11/06 PHP
php+ajax实时刷新简单实例
2015/02/25 PHP
PHP开发Apache服务器配置
2015/07/15 PHP
基于swoole实现多人聊天室
2018/06/14 PHP
基于PHP+Mysql简单实现了图书购物车系统的实例详解
2020/08/06 PHP
extjs grid取到数据而不显示的解决
2008/12/29 Javascript
javascript concat数组累加 示例
2009/09/03 Javascript
js编写trim()函数及正则表达式的运用
2013/10/24 Javascript
JQuery实现样式设置、追加、移除与切换的方法
2015/06/11 Javascript
js鼠标点击图片切换效果实现代码
2015/11/19 Javascript
JS获取鼠标坐标位置实例分析
2016/01/20 Javascript
移动端日期插件Mobiscroll.js使用详解
2016/12/19 Javascript
JavaScript输出所选择起始与结束日期的方法
2017/07/12 Javascript
使用JavaScript实现点击循环切换图片效果
2017/09/03 Javascript
利用angular自动编译andriod APK的绕坑经历分享
2019/03/08 Javascript
微信小程序使用 vant Dialog组件的正确方式
2020/02/21 Javascript
python中base64加密解密方法实例分析
2015/05/16 Python
Python 专题四 文件基础知识
2017/03/20 Python
python中安装Scrapy模块依赖包汇总
2017/07/02 Python
深度辨析Python的eval()与exec()的方法
2019/03/26 Python
python使用装饰器作日志处理的方法
2019/07/11 Python
Python中关于浮点数的冷知识
2019/09/22 Python
django 解决自定义序列化返回处理数据为null的问题
2020/05/20 Python
python如何调用字典的key
2020/05/25 Python
使用CSS3来实现滚动视差效果的教程
2015/08/24 HTML / CSS
军校大学生个人的自我评价
2014/02/17 职场文书
《1942》观后感
2015/06/08 职场文书
可可西里观后感
2015/06/08 职场文书
住房公积金贷款工资证明
2015/06/12 职场文书
高三数学复习备考教学反思
2016/02/18 职场文书
SQL Server使用T-SQL语句批处理
2022/05/20 SQL Server