本文實例講述了Python實現獲取本地及遠程圖片大小的方法。分享給大家供大家參考,具體如下:
了解過Pillow
的都知道,Pillow
是一個非常強大的圖片處理器,這篇文章主要記錄一下Pillow
對圖片信息的獲?。?/p>
安裝Pillow
1
|
pip install pillow |
本地圖片
1
2
3
4
5
6
7
8
|
# -*- coding:utf-8 -*- #! python2 import os from PIL import Image path = os.path.join(os.getcwd(), "23.png" ) img = Image. open (path) print img. format # PNG print img.size # (3500, 3500) |
遠程圖片
1
2
3
4
5
6
7
8
9
10
11
|
# -*- coding:utf-8 -*- #! python2 import urllib2 import cStringIO from PIL import Image path = "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 # JPEG print img.size # (801, 1200) |
運行結果如下圖:
希望本文所述對大家Python程序設計有所幫助。
原文鏈接:https://blog.csdn.net/y472360651/article/details/79272927