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

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

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

本文實例講述了python使用PIL縮放網(wǎng)絡(luò)圖片并保存的方法。分享給大家供大家參考。具體實現(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()

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

相關(guān)文章

  • Python的條件控制?if?語句詳解

    Python的條件控制?if?語句詳解

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

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

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

    Django實現(xiàn)登錄隨機驗證碼的示例代碼

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

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

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

    Pytorch的安裝過程之pip、conda、Docker容器安裝

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

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

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

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

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

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

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

    解決Pycharm界面的子窗口不見了的問題

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

    Python用sndhdr模塊識別音頻格式詳解

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

最新評論

城步| 前郭尔| 扎赉特旗| 射洪县| 长乐市| 罗江县| 柳州市| 云南省| 和田县| 泸定县| 闵行区| 新源县| 宜君县| 理塘县| 上虞市| 南皮县| 城口县| 舒城县| 朝阳市| 平定县| 文昌市| 伊金霍洛旗| 九台市| 曲水县| 宁国市| 盐边县| 大厂| 龙陵县| 九江市| 出国| 西安市| 邯郸县| 东山县| 隆子县| 当涂县| 舟曲县| 宝清县| 贺州市| 离岛区| 通州区| 偏关县|