Python PIL繪制矩形框的常用方法總結(jié)
基礎(chǔ)代碼
from PIL import Image, ImageDraw
# 打開圖片
img = Image.open('your_image.jpg')
# 創(chuàng)建繪圖對象
draw = ImageDraw.Draw(img)
# 矩形坐標 (x1, y1, x2, y2)
coords = (23, 21, 69, 76)
# 畫矩形框(紅色,線寬2)
draw.rectangle(coords, outline='red', width=2)
# 保存圖片
img.save('output.jpg')
img.show()
效果說明
| 參數(shù) | 說明 |
|---|---|
coords = (23, 21, 69, 76) | 左上角(23,21),右下角(69,76) |
outline='red' | 邊框顏色 |
width=2 | 邊框粗細 |
更多用法
1.填充矩形(實心)
draw.rectangle(coords, fill='blue', outline='red', width=2)
2.半透明矩形(Pillow 9.0+)
draw.rectangle(coords, fill=(255, 0, 0, 128), outline='red', width=2) # RGBA,最后一個值是透明度 0-255
3.多種顏色樣式
# 綠色虛線框 draw.rectangle(coords, outline='green', width=3) # 黃色粗框 draw.rectangle(coords, outline='yellow', width=5) # 白色細框 draw.rectangle(coords, outline='white', width=1)
完整示例(含新建圖片)
from PIL import Image, ImageDraw
# 如果沒有圖片,可以新建一個
img = Image.new('RGB', (200, 200), color='white')
draw = ImageDraw.Draw(img)
# 畫矩形
draw.rectangle((23, 21, 69, 76), outline='red', width=2)
img.save('result.jpg')
img.show()
坐標示意
(23,21) ───────── (69,21)
│ │
│ 矩形區(qū)域 │
│ │
(23,76) ───────── (69,76)
提示:如果圖片路徑包含中文,建議用 Image.open(r'路徑') 或處理編碼問題。
知識擴展
在 Python 中用 PIL(實際是它的升級版 Pillow)繪制矩形框是非常簡單的。通過 ImageDraw.Draw 對象的 rectangle() 方法即可實現(xiàn)。
1. 安裝 Pillow
pip install Pillow
2. 基本用法
from PIL import Image, ImageDraw
# 創(chuàng)建一個 400x300 的白色圖像(RGB模式)
img = Image.new('RGB', (400, 300), color='white')
# 獲取繪圖上下文
draw = ImageDraw.Draw(img)
# 繪制矩形框:(x1, y1) 左上角,(x2, y2) 右下角
draw.rectangle((50, 50, 350, 250), outline='red', width=3)
# 顯示或保存圖像
img.show()
img.save('rectangle.png')3. 參數(shù)詳解
rectangle(xy, fill=None, outline=None, width=1)
| 參數(shù) | 說明 |
|---|---|
xy | 矩形坐標,可接受 (x1, y1, x2, y2) 或 [(x1,y1), (x2,y2)] 兩種形式 |
fill | 填充顏色,如 'blue'、(0,255,0)、'#FF00FF'。None 表示不填充 |
outline | 邊框顏色,同上。若為 None 則不繪制邊框 |
width | 邊框?qū)挾龋ㄏ袼兀?,默認為 1 |
4. 示例:實心矩形 + 邊框
draw.rectangle((50, 50, 150, 150), fill='lightblue', outline='black', width=3)
5. 示例:多個矩形,支持 RGBA 透明度
from PIL import Image, ImageDraw
img = Image.new('RGBA', (400, 300), color=(255,255,255,0)) # 透明背景
draw = ImageDraw.Draw(img)
# 半透明填充(需要 RGBA 模式)
draw.rectangle((30, 30, 180, 130), fill=(255,0,0,128), outline=(0,0,0,255))
draw.rectangle((200, 50, 350, 200), outline='green', width=4)
img.save('rect_alpha.png')6. 在現(xiàn)有圖像上繪制矩形框(例如標注人臉)
from PIL import Image, ImageDraw
img = Image.open('photo.jpg')
draw = ImageDraw.Draw(img)
# 在坐標區(qū)域繪制紅色框
draw.rectangle((120, 80, 250, 210), outline='red', width=5)
img.show()
img.save('annotated.jpg')7. 使用相對坐標(通過 bounding box)
# 對 PIL 圖像對象本身獲取寬度高度 width, height = img.size left = width * 0.2 top = height * 0.2 right = width * 0.8 bottom = height * 0.8 draw.rectangle((left, top, right, bottom), outline='yellow', width=4)
總結(jié)
- 核心是
ImageDraw.Draw的rectangle()方法。 - 可以自由指定顏色、邊框?qū)挾?、是否填充?/li>
- 支持 RGB / RGBA 顏色模式,實現(xiàn)透明或半透明效果。
- 配合
Image.new()可以創(chuàng)建全新矩形圖像,配合Image.open()可以在已有圖片上標注。
到此這篇關(guān)于Python PIL繪制矩形框的常用方法總結(jié)的文章就介紹到這了,更多相關(guān)Python PIL繪制矩形框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jupyter note 實現(xiàn)將數(shù)據(jù)保存為word
這篇文章主要介紹了jupyter note 實現(xiàn)將數(shù)據(jù)保存為word,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python中np.random.randint()參數(shù)詳解及用法實例
numpy.random.randint()函數(shù)不僅可以生成一維隨機數(shù)組,也可以生成多維度的隨機數(shù)組,下面這篇文章主要給大家介紹了關(guān)于Python中np.random.randint()參數(shù)詳解及用法的相關(guān)資料,需要的朋友可以參考下2022-09-09
手把手教你部署AI小說生成器(Python環(huán)境搭建)
本文詳細介紹了如何使用Python+AI從零開始搭建屬于自己的AI小說生成器,文章分為環(huán)境準備、第一次安裝、關(guān)機重啟、配置大腦等步驟進行講解,幫助用戶輕松實現(xiàn)AI輔助創(chuàng)作2026-04-04
python串口如何讀取byte類型數(shù)據(jù)并訪問
這篇文章主要介紹了python串口如何讀取byte類型數(shù)據(jù)并訪問方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09

