最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python使用PIL縮放網(wǎng)絡(luò)圖片并保存的方法

 更新時(shí)間:2015年04月24日 14:53:02   作者:feiwen  
這篇文章主要介紹了python使用PIL縮放網(wǎng)絡(luò)圖片并保存的方法,涉及Python操作網(wǎng)絡(luò)圖片的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了python使用PIL縮放網(wǎng)絡(luò)圖片并保存的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

''' 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()

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Python的條件控制?if?語(yǔ)句詳解

    Python的條件控制?if?語(yǔ)句詳解

    Python的?if?語(yǔ)句用來(lái)「控制代碼」的執(zhí)行,「判斷條件成立」時(shí)執(zhí)行一段代碼,判斷條件「不成立」時(shí)執(zhí)行另一段代碼,本文就給大家詳細(xì)講講Python的條件控制?if?語(yǔ)句,需要的朋友可以參考下
    2023-08-08
  • python中requests模擬登錄的三種方式(攜帶cookie/session進(jìn)行請(qǐng)求網(wǎng)站)

    python中requests模擬登錄的三種方式(攜帶cookie/session進(jìn)行請(qǐng)求網(wǎng)站)

    這篇文章主要介紹了python中requests模擬登錄的三種方式(攜帶cookie/session進(jìn)行請(qǐng)求網(wǎng)站),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Django實(shí)現(xiàn)登錄隨機(jī)驗(yàn)證碼的示例代碼

    Django實(shí)現(xiàn)登錄隨機(jī)驗(yàn)證碼的示例代碼

    登錄驗(yàn)證碼是每個(gè)網(wǎng)站登錄時(shí)的基本標(biāo)配,這篇文章主要介紹了Django實(shí)現(xiàn)登錄隨機(jī)驗(yàn)證碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割

    python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割

    這篇文章主要為大家詳細(xì)介紹了python多進(jìn)程下實(shí)現(xiàn)日志記錄按時(shí)間分割,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Pytorch的安裝過(guò)程之pip、conda、Docker容器安裝

    Pytorch的安裝過(guò)程之pip、conda、Docker容器安裝

    PyTorch是一個(gè)基于Python的開源深度學(xué)習(xí)框架,可用于訓(xùn)練和預(yù)測(cè)深度學(xué)習(xí)模型,PyTorch支持多種安裝方法,這篇文章主要介紹了Pytorch的安裝----pip、conda、Docker容器,需要的朋友可以參考下
    2023-04-04
  • 詳解Django的CSRF認(rèn)證實(shí)現(xiàn)

    詳解Django的CSRF認(rèn)證實(shí)現(xiàn)

    這篇文章主要介紹了詳解Django的CSRF認(rèn)證實(shí)現(xiàn),詳細(xì)的介紹了csrf原理和實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • python Cartopy的基礎(chǔ)使用詳解

    python Cartopy的基礎(chǔ)使用詳解

    這篇文章主要介紹了python Cartopy的基礎(chǔ)使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python PEP8代碼規(guī)范常見問(wèn)題以及解決方案

    Python PEP8代碼規(guī)范常見問(wèn)題以及解決方案

    PEP8是Python的代碼規(guī)范,本文總結(jié)了常見的PEP8代碼規(guī)范問(wèn)題及解決方法,幫助開發(fā)者編寫規(guī)范的代碼
    2024-11-11
  • 解決Pycharm界面的子窗口不見了的問(wèn)題

    解決Pycharm界面的子窗口不見了的問(wèn)題

    今天小編就為大家分享一篇解決Pycharm界面的子窗口不見了的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Python用sndhdr模塊識(shí)別音頻格式詳解

    Python用sndhdr模塊識(shí)別音頻格式詳解

    這篇文章主要介紹了Python用sndhdr模塊識(shí)別音頻格式詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01

最新評(píng)論

荃湾区| 静乐县| 永丰县| 永康市| 铅山县| 彭泽县| 平阳县| 翁牛特旗| 上林县| 个旧市| 石嘴山市| 突泉县| 祁连县| 伊金霍洛旗| 内江市| 白河县| 威信县| 仁化县| 合水县| 治县。| 滁州市| 特克斯县| 夏河县| 武义县| 中超| 永泰县| 高陵县| 洞头县| 慈利县| 丽江市| 顺义区| 双牌县| 普定县| 宣武区| 山阴县| 莱阳市| 梧州市| 犍为县| 眉山市| 双流县| 宝丰县|