Python 處理圖片python
PIL 是 Python 最經常使用的圖像處理庫,在 Python 2.x 中是 PIL 模塊,在 Python 3.x 中已經替換成 pillow 模塊,安裝 PIL :pip install pillowspa
一、Python 查看圖片的一些屬性code
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 from PIL import Image 5 6 image = Image.open("dora.jpg") # 打開一個圖片對象 7 print(image.format) # 查看圖片的格式,結果爲'JPEG' 8 print(image.size) # 查看圖片的大小,結果爲(800,450)分別表示寬和高的像素 9 print(image.mode) # 查看圖片的模式,結果爲'RGB',其餘模式還有位圖模式、灰度模式、雙色調模式等等
二、Python 調整圖片大小orm
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 from PIL import Image 5 6 image = Image.open("dora.jpg") # 打開一個圖片對象 7 out = image.resize((128, 128)) # 把圖片調整爲寬128像素,高128像素 8 out.show() # 查看圖片
效果圖:對象
三、Python 旋轉圖片blog
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 from PIL import Image 5 6 image = Image.open("dora.jpg") # 打開一個圖片對象 7 out = image.rotate(45) # 將圖片逆時針旋轉45度 8 out.show() # 查看圖片
效果圖:圖片
四、Python 翻轉圖片ip
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 from PIL import Image 5 6 image = Image.open("dora.jpg") # 打開一個圖片對象 7 out = image.transpose(Image.FLIP_LEFT_RIGHT) # 左右翻轉圖片,若是要上下翻轉參數爲'Image.FLIP_TOP_BOTTOM' 8 out.show() # 查看圖片
效果圖:utf-8