当前位置:   article > 正文

Python 通过ImageDraw.rectangle 画矩形框

draw.rectangle

1. 函数讲解

源代码如下:ImageDraw.rectangle(xy, fill=None, outline=None, width=1)
主要的参数如下:

  • xy: 定义边界框长宽边界,主要格式如下:[(x0, y0), (x1, y1)]或者 [x0, y0, x1, y1]
  • fill:填充颜色
  • outline:轮廓颜色
  • width:矩形边框的宽度

2. 示例代码

# importing image object from PIL 
import math 
from PIL import Image, ImageDraw 
  
img = Image.open("Gym.jpg") 

ImageDraw.Draw(img).rectangle([(100, 300), (300, 700)] , fill =None, outline ="red",width =2) 

plt.imshow(img)
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

截图如下:

在这里插入图片描述

3. 补充cv2.rectangle

cv2.rectangle 则根据对角线画矩形

示例代码如下:

import os
import cv2
import matplotlib.pyplot as plt

img = cv2.imread("Gym.jpg")
xmin = 100
xmax = 500
ymin = 100
ymax = 800
cv2.rectangle(img, (xmin, ymin), (xmax, ymax), (0,0,255), 2)
cv2.rectangle(img, (xmin, ymax), (xmax, ymin), (255,0,0), 2)
#     cv2.imshow('src',img)
#     cv2.waitKey()

plt.imshow(img)
plt.show()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

截图如下:
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/742939
推荐阅读
相关标签
  

闽ICP备14008679号