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

 更新时间:2015年04月24日 14:53:02   作者:feiwen  
这篇文章主要介绍了python使用PIL缩放网络图片并保存的方法,涉及Python操作网络图片的相关技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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的条件控制 if 语句详解

    Python的条件控制 if 语句详解

    Python的 if 语句用来「控制代码」的执行,「判断条件成立」时执行一段代码,判断条件「不成立」时执行另一段代码,本文就给大家详细讲讲Python的条件控制 if 语句,需要的朋友可以参考下
    2023-08-08
  • python中requests模拟登录的三种方式(携带cookie/session进行请求网站)

    python中requests模拟登录的三种方式(携带cookie/session进行请求网站)

    这篇文章主要介绍了python中requests模拟登录的三种方式(携带cookie/session进行请求网站),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Django实现登录随机验证码的示例代码

    Django实现登录随机验证码的示例代码

    登录验证码是每个网站登录时的基本标配,这篇文章主要介绍了Django实现登录随机验证码的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • python多进程下实现日志记录按时间分割

    python多进程下实现日志记录按时间分割

    这篇文章主要为大家详细介绍了python多进程下实现日志记录按时间分割,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-07-07
  • Pytorch的安装过程之pip、conda、Docker容器安装

    Pytorch的安装过程之pip、conda、Docker容器安装

    PyTorch是一个基于Python的开源深度学习框架,可用于训练和预测深度学习模型,PyTorch支持多种安装方法,这篇文章主要介绍了Pytorch的安装----pip、conda、Docker容器,需要的朋友可以参考下
    2023-04-04
  • 详解Django的CSRF认证实现

    详解Django的CSRF认证实现

    这篇文章主要介绍了详解Django的CSRF认证实现,详细的介绍了csrf原理和实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-10-10
  • python Cartopy的基础使用详解

    python Cartopy的基础使用详解

    这篇文章主要介绍了python Cartopy的基础使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Python PEP8代码规范常见问题以及解决方案

    Python PEP8代码规范常见问题以及解决方案

    PEP8是Python的代码规范,本文总结了常见的PEP8代码规范问题及解决方法,帮助开发者编写规范的代码
    2024-11-11
  • 解决Pycharm界面的子窗口不见了的问题

    解决Pycharm界面的子窗口不见了的问题

    今天小编就为大家分享一篇解决Pycharm界面的子窗口不见了的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python用sndhdr模块识别音频格式详解

    Python用sndhdr模块识别音频格式详解

    这篇文章主要介绍了Python用sndhdr模块识别音频格式详解,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01

最新评论