Python PIL(图像处理库)使用方法
生活随笔
收集整理的這篇文章主要介紹了
Python PIL(图像处理库)使用方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
介紹
PIL:Python Imaging Library,已經是Python平臺事實上的圖像處理標準庫了。?
from PIL import Image, ImageDraw
安裝
pip3 install "pillow==6.2.0"
Image 對于圖像的各種操作?
ImageDraw 模塊提供了圖像對象的簡單2D繪制。用戶可以使用這個模塊創建新的圖像,注釋或潤飾已存在圖像,為web應用實時產生各種圖形。
讀入圖像數據
Image.read(path) 讀入圖片
圖片信息
| Image.format | 圖像文件格式(后綴) |
| im.size | (width,height) |
| Image.mode | 像素格式 RGB… |
圖片處理
| Image.thumbnail((w//2, h//2)) | 縮放 |
| out = im.resize((128, 128)) | 重定義大小 |
| out = im.rotate(45) | 旋轉 |
| im.filter(ImageFilter.BLUR) | 模糊 |
| Image.show() | 顯示圖像(標準實現不是很有效率)不過測試可用 |
| regionregion.transpose(Image.ROTATE_180) | 旋轉 |
| box = (100, 100, 400, 400) region = im.crop(box) | 獲取子區域 |
| Image.paste(region, box) | 處理完子區域粘貼回去 |
| r, g, b = im.split() | 分離圖像通道 |
| im = Image.merge(“RGB”, (b, g, r)); | 合并圖像通道 |
| Image.mode(‘/Users/michael/thumbnail.jpg’, ‘jpeg’) | 保存 |
ImageDraw
#給圖像繪制一個矩形框 sample_image_roi = sample_image.copy() fillcolor=(255,0,0) draw = ImageDraw.Draw(sample_image_roi) points = [(1,76), (1,135), (255,135), (255,76)] for i in range(0, len(points), 1):draw.line([points[i], points[(i+1)%len(points)]], fill=fillcolor, width=3) del draw #刪除變量plt.title('Image with sample ROI') plt.imshow(sample_image_roi) plt.show()參考文檔
The Python Imaging Library Handbook
總結
以上是生活随笔為你收集整理的Python PIL(图像处理库)使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Web完整渗透测试实例(windows)
- 下一篇: Linux 主机超全渗透测试命令汇总