python--在图片上画矩形框(目标检测)

本文介绍如何利用Python的Matplotlib和PIL库在图像上绘制矩形。通过Rectangle函数和ImageDraw模块,可以指定矩形的位置、大小及颜色,适用于图像处理和数据可视化场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

matplotlib库

Rectangle((左上角x,左上角y),宽,高)

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
rect = plt.Rectangle((top_left_x, top_left_y), width, height, fill=False, edgecolor = 'red',linewidth=1)
ax.add_patch(rect)
plt.imshow(image_array) # 图像数组
plt.show()

在这里插入图片描述

PIL库

from PIL import ImageDraw
image = Image.open(path) # 打开一张图片
draw = ImageDraw.Draw(image) # 在上面画画
draw.rectangle([645,465,200,200], outline=(255,0,0)) # [左上角x,左上角y,右下角x,右下角y],outline边框颜色
image.show() 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值