• 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 实现文件的全备份和差异备份详解
  • Python中几个比较常见的名词解释
  • 编写Python脚本抓取网络小说来制作自己的阅读器
  • Python中的下划线详解
  • Python中的赋值、浅拷贝、深拷贝介绍
  • Windows安装Python、pip、easy_install的方法
  • python抓取网页中的图片示例
  • Python中使用OpenCV库来进行简单的气象学遥感影像计算
  • 一个基于flask的web应用诞生 记录用户账户登录状态(6)

文章分类

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

最近更新的内容

    • Python3基础之基本数据类型概述
    • Python彩色化Linux的命令行终端界面的代码实例分享
    • json跨域调用python的方法详解
    • 在CentOS上配置Nginx+Gunicorn+Python+Flask环境的教程
    • 为python设置socket代理的方法
    • python完成FizzBuzzWhizz问题(拉勾网面试题)示例
    • Python编程中字符串和列表的基本知识讲解
    • Python中死锁的形成示例及死锁情况的防止
    • python3图片转换二进制存入mysql
    • 简介Django框架中可使用的各类缓存

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

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