本文實例講述了Python實現獲取本地及遠程圖片大小的方法。分享給大家供大家參考,具體如下:
了解過Pillow
的都知道,Pillow
是一個非常強大的圖片處理器,這篇文章主要記錄一下Pillow
對圖片信息的獲取:
安裝Pillow
pip install pillow
本地圖片
# -*- coding:utf-8 -*-#! python2import osfrom PIL import Imagepath = os.path.join(os.getcwd(),"23.png")img = Image.open(path)print img.format # PNGprint img.size # (3500, 3500)
遠程圖片
# -*- coding:utf-8 -*-#! python2import urllib2import cStringIOfrom PIL import Imagepath = "http://h.hiphotos.baidu.com/image/pic/item/c8ea15ce36d3d5397966ba5b3187e950342ab0cb.jpg"file = urllib2.urlopen(path)tmpIm = cStringIO.StringIO(file.read())img = Image.open(tmpIm)print img.format # JPEGprint img.size # (801, 1200)
運行結果如下圖:
更多關于Python相關內容可查看本站專題:《Python數學運算技巧總結》、《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》及《Python入門與進階經典教程》
希望本文所述對大家Python程序設計有所幫助。
|
新聞熱點
疑難解答