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运行效率
Jun 09 Python
Python 详解基本语法_函数_返回值
Jan 22 Python
Python如何生成树形图案
Jan 03 Python
Python使用SQLite和Excel操作进行数据分析
Jan 20 Python
Python设计模式之抽象工厂模式原理与用法详解
Jan 15 Python
numpy数组之存取文件的实现示例
May 24 Python
PyCharm 2019.3发布增加了新功能一览
Dec 08 Python
tensorflow 20:搭网络,导出模型,运行模型的实例
May 26 Python
mac安装python3后使用pip和pip3的区别说明
Sep 01 Python
python 实现关联规则算法Apriori的示例
Sep 30 Python
python生成随机数、随机字符、随机字符串
Apr 06 Python
Python中re模块的元字符使用小结
Apr 07 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使用pdo连接mssql server数据库实例
2014/12/25 PHP
Laravel服务容器绑定的几种方法总结
2020/06/14 PHP
javascript 表单的友好用户体现
2009/01/07 Javascript
使用JavaScript动态设置样式实现代码(2)
2013/01/25 Javascript
js获取URL的参数的方法(getQueryString)示例
2013/09/29 Javascript
JS仿百度搜索自动提示框匹配查询功能
2013/11/21 Javascript
jquery禁止回车触发表单提交
2014/12/12 Javascript
JavaScript 面向对象与原型
2015/04/10 Javascript
高性能JavaScript 重排与重绘(2)
2015/08/11 Javascript
JavaScript如何实现在文本框(密码框)输入提示语
2015/12/25 Javascript
jQuery实现可以控制图片旋转角度效果(附demo源码下载)
2016/01/27 Javascript
angular和BootStrap3实现购物车功能
2017/01/25 Javascript
JavaScript hasOwnProperty() 函数实例详解
2017/08/04 Javascript
bootstrap-table.js扩展分页工具栏(增加跳转到xx页)功能
2017/12/28 Javascript
微信小程序自定义tab实现多层tab嵌套功能
2018/06/15 Javascript
前端Vue项目详解--初始化及导航栏
2019/06/24 Javascript
JS 图片压缩原理与实现方法详解
2020/04/29 Javascript
简单了解JavaScript弹窗实现代码
2020/05/07 Javascript
JS前端基于canvas给图片添加水印
2020/11/11 Javascript
详解Python装饰器由浅入深
2016/12/09 Python
PyTorch 1.0 正式版已经发布了
2018/12/13 Python
Django文件存储 自己定制存储系统解析
2019/08/02 Python
Python使用Slider组件实现调整曲线参数功能示例
2019/09/06 Python
详解CSS3中@media的实际使用
2015/08/04 HTML / CSS
使用CSS3编写灰阶滤镜来制作黑白照片效果的方法
2016/05/09 HTML / CSS
CSS3实现酷炫的3D旋转透视效果
2019/11/21 HTML / CSS
很酷的HTML5电子书翻页动画特效
2016/02/25 HTML / CSS
HTML5各种头部meta标签的功能(推荐)
2017/03/13 HTML / CSS
Carter’s官方旗舰店:美国受欢迎的婴童服装品牌
2018/01/21 全球购物
荷兰鞋类购物网站:Donelli
2019/05/24 全球购物
工伤事故处理协议书怎么写
2014/10/15 职场文书
不同意离婚上诉状
2015/05/23 职场文书
2015年行政管理人员工作总结
2015/10/15 职场文书
校长新学期寄语2016
2015/12/04 职场文书
选对餐饮营销策略,营业额才会上涨
2019/08/27 职场文书
用Python可视化新冠疫情数据
2022/01/18 Python