1. 使用Pillow库:
from PIL import Image, ImageDraw
加载图像
image = Image.open("example.jpg")
创建画布对象
draw = ImageDraw.Draw(image)
绘制方框
draw.rectangle((x1, y1, x2, y2), outline="red", width=2)
保存和显示图像
image.save("output.jpg")
image.show()
2. 使用OpenCV库:
import cv2
读取图像
image = cv2.imread("example.jpg")
绘制方框
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
保存和显示图像
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
请根据您的需求选择合适的库和代码示例。