• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python使用PIL缩放网络图片并保存的方法

python使用PIL缩放网络图片并保存的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python3 pil,python pil,python中pil,python pil安装,python3.5 pil等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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()
</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python实现更改图片尺寸大小的方法(基于Pillow包)
  • python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法
  • Python基于pillow判断图片完整性的方法
  • Python基于pillow判断图片完整性的方法
  • Linux上安装Python的PIL和Pillow库处理图片的实例教程
  • python中PIL安装简单教程
  • Python使用PIL库实现验证码图片的方法
  • Python使用PIL库实现验证码图片的方法
  • 使用Python的PIL模块来进行图片对比
  • 使用Python的PIL模块来进行图片对比

相关文章

  • Python中基本的日期时间处理的学习教程
  • Python内置函数OCT详解
  • Python logging模块学习笔记
  • 举例讲解Linux系统下Python调用系统Shell的方法
  • 详解Python的Django框架中的templates设置
  • 使用Python编写vim插件的简单示例
  • 在Django的视图(View)外使用Session的方法
  • Python的字典和列表的使用中一些需要注意的地方
  • python中MySQLdb模块用法实例
  • python操作ssh实现服务器日志下载的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Pythont特殊语法filter,map,reduce,apply使用方法
    • django之常用命令详解
    • python算法学习之计数排序实例
    • Python-基础-入门 简介
    • 在Python中使用dict和set方法的教程
    • Python基于scrapy采集数据时使用代理服务器的方法
    • python 统计代码行数简单实例
    • Python文件操作类操作实例详解
    • windows系统下Python环境搭建教程
    • python实现的一个火车票转让信息采集器

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有