1.先看一段英文代碼The Python Imaging Library (PIL) adds image processing capabilities to your Python interpreter. This library supports many file formats, and provides powerful image processing and graphics capabilities,意思是說PIL給Python增加了圖像處理功能,這個庫可以處理多種文件格式圖像,提供了強大的圖像處理和圖形處理能力。
2.安裝PIL,目前一般的PIL版本只支持python2.x版本,如果想用3.0以后的,先下載anaconda這個平台,使用起來分方便,自帶spyder和qtpython插件,下載地址見https://repo.continuum.io/archive/,里面有各種平台的安裝包,根據你的平台具體下載,這個平台安裝好后可以很方便下載很多python支持庫,本人用的Anaconda3這個版本系列。
3.這個PIL使用起來很精單首先導入模塊:from PIL import Image,並查看模塊內容,里面有好多組件,Image就是一個常用組件
from PIL import Image
dir(PIL) Out[17]: ['BmpImagePlugin', 'GifImagePlugin', 'GimpGradientFile', 'GimpPaletteFile', 'Image', 'ImageChops', 'ImageColor', 'ImageEnhance', 'ImageFile', 'ImageFilter', 'ImageMode', 'ImagePalette', 'ImageSequence', 'ImageStat', 'JpegImagePlugin', 'JpegPresets', 'PILLOW_VERSION', 'PaletteFile', 'PngImagePlugin', 'PpmImagePlugin', 'TiffImagePlugin', 'TiffTags', 'VERSION', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_binary', '_imaging', '_plugins', '_util', 'version']
3.打開一幅圖像:open(fp, mode="r")第一個參數為文件路徑,第二個為打開模式一般默認只讀
img = Image.open('girl1.jpg')
4.將打開的圖像顯示出來調用:show(self, title=None, command=None), On Windows, it saves the image to a temporary BMP file, and usesthe standard BMP display utility to show it (usually Paint).:param title: Optional title to use for the image window,where possible.:param command: command used to show the image,一般會產生一幅臨時圖像,然后調用默認的圖像查看器顯示圖像
img.show()

5.查看圖像尺寸img.size
w,h = img.size
w
Out[27]: 1920
h
Out[28]: 1080
6.縮放圖像img:thumbnail(self, size, resample=BICUBIC),第二個參數是縮放尺寸,第三個參數可以指定縮放模式,有`PIL.Image.NEAREST`, `PIL.Image.BILINEAR`,`PIL.Image.BICUBIC`, `PIL.Image.LANCZOS`.這四種模式
In:img.thumbnail((w/2,h/2))
In:img.size
Out[33]: (960, 540)

7.保存圖像save(self, fp, format=None, **params)
img.save('D:\\girl.jpeg','jpeg')
8.根據所給的模式和尺寸創建一幅新圖像new(mode, size, color=0),模式,尺寸,顏色

9.選中圖片一部分進行復制crop(self, box=None) The crop rectangle, as a (left, upper, right, lower)-第二個參數為一個矩形也就是一個四個元素的元組

10.粘貼圖像paste(self, im, box=None, mask=None)

