python有一個圖像處理庫——PIL,可以處理圖像文件。PIL提供了功能豐富的方法,比如格式轉換、旋轉、裁剪、改變尺寸、像素處理、圖片合並等等等等,非常強大。
首先需要安裝這個圖像處理庫。
舉個簡單的例子,調整圖片的大小:
import Image infile = 'D:\\original_img.jpg' outfile = 'D:\\adjust_img.jpg' im = Image.open(infile) (x,y) = im.size #read image size x_s = 250 #define standard width y_s = y * x_s / x #calc height based on standard width out = im.resize((x_s,y_s),Image.ANTIALIAS) #resize image with high-quality out.save(outfile) print 'original size: ',x,y print 'adjust size: ',x_s,y_s ''' OUTPUT: original size: 500 358 adjust size: 250 179 '''
原始圖片
調整后圖片
更多更詳細的函數見PIL使用文檔:
PIL文檔下載
PIL_1.1.7_win32_py2.7 下載
以上安裝成功之后仍然有錯誤報出:ImportError: The _imaging C module is not installed錯誤解決記錄。
網上提示在此http://www.cnblogs.com/myx/p/The_imaging-C-module-is-not-installed.html網站說到http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil下載安裝下面的whl就可以
Pillow‑2.7.0‑cp27‑none‑win_amd64.whl
下載后pip install Pillow‑2.7.0‑cp27‑none‑win_amd64.whl,仍然有錯。暫時本地windows先不管了,到linux服務器再說吧(好吧,其實已經好了,只是沒有覺察到,囧囧囧)。
