做图像识别的时候需要在图片中画出特定大小和角度的矩形框,自己写了一个函数,给定的输入是图片名称,矩形框的位置坐标,长宽和角度,直接输出画好矩形框的图片。
主要思想是先根据x,y坐标和长宽得到矩形,然后通过数学计算得到旋转angle角度后的新矩形框的四个顶点位置坐标,再利用draw.line()函数画出来。
import math
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image, ImageDraw
def draw(filename,result):
img = Image.open(fi