pip install pillow
from PIL import pillow
picture = Image.open('test.jpg')
picture = Image.new('RGB', (200, 200), 'red')
第一個參數是mode即顏色空間模式,第二個參數指定了圖像的分辨率(寬x高),第三個參數是顏色。python
spa
也能夠填入十六進制表示的顏色,如#FF0000
表示紅色。code
還能傳入元組,好比(255, 0, 0, 255)或者(255, 0, 0)表示紅色。 orm
picture.save('test.jpg')
(左, 頂, 右, 底)blog
# 文件名 print(image.filename) # 文件格式 print(image.format) # 文件模式 print(image.mode) # 文件大小 print(image.size) # 文件寬度 print(image.width) # 文件高度 print(image.height) # 文件信息 print(image.info)
# 剪切 croped_im = image.crop((0, 0, 200, 200)) croped_im.save('14.jpg') # 複製 copy_im = croped_im.copy() copy_im.save('13.jpg') # 粘貼 croped_im = image.crop((0, 0, 300, 300)) croped_im.show() image.paste(croped_im, (100, 100)) image.save('12.jpg') # 調整大小 resized_im = image.resize((683, 728)) resized_im.show() # 調整大小,(製造縮略圖) w, h = image.size image.thumbnail((w//2, h//2)) image.show() # 旋轉圖像 image = image.rotate(45) image.show() image = image.rotate(45, expand=100) image.show() # 翻轉圖像 image = image.transpose(Image.FLIP_LEFT_RIGHT) image.show() image = image.transpose(Image.FLIP_TOP_BOTTOM) image.show() # 獲取圖片通道名稱 image = image.getbands() print(image) # 經過通道切割圖片 R, G, B = image.split() R.show() G.show() B.show() print((R, G, B)) # 獲取單個通道的圖片 R = image.getchannel('R') R.show() # 模式轉化 image = image.convert('L') image.show() # 獲取單個像素值 image = image.getpixel((100,100)) print(image) # 加載圖片所有數據 pixdata = image.load() pixdata[1,1] = 255, 255, 255 image.show() print(pixdata) print(pixdata[0,0]) print(type(pixdata[0,1])) print([i for x in range(1) for y in range(1) for i in pixdata[x, y]]) # 獲取全部像素內容 image = image.getdata() image = image.getdata(band=0) image = image.getdata(band=1) image = image.getdata(band=2) print(image) print(list(image)[0]) # 關閉圖片 image.show() image.close()