如何在ImageDraw中绘制倾斜椭圆?

0 投票
1 回答
2377 浏览
提问于 2025-04-17 00:16

我想在图像上画一个倾斜的椭圆。但是我不太确定该怎么定义它,因为下面的方案虽然可以移动点,但我觉得这只是把椭圆压扁了,并没有旋转它(而且我觉得这个变换本身也有点问题)。我把这个函数的输出结果传给椭圆命令,然后把它加到已有的图片上,所以任何旋转整个图像的方法都不适用。OD只是一个偏移到我使用的坐标中心的正方形。

def ellipsebound(major, minor, tilt=0, offset=0,  angle=0):
    #creates a bound for an ellispe, defined with tilt meaning to rotate the orthogonal axis and angle corresponds to rotating the ellipse position
    angle = radians(angle)
    tilt = radians(tilt)
    box=(
    1 + int(ceil((OD+offset*cos(angle)+(major*cos(tilt)+minor*sin(tilt)))/conv)), 
    1 + int(ceil((OD+offset*sin(angle)+(major*sin(tilt)-minor*cos(tilt)))/conv)),
 int(ceil((2*OD-(OD-offset*cos(angle)-(major*cos(tilt)+minor*sin(tilt)))/conv))),
 int(ceil((2*OD-(OD-offset*sin(angle)-(major*sin(tilt)-minor*cos(tilt)))/conv)))
 ) #create bounding box
    return box

有没有人知道怎么做到这一点?

1 个回答

3

看起来用来画椭圆的那个“框”并没有旋转的设置。它只是通过(左,上,右,下)这些边界来定义的。

一个可能的解决办法(这要看你具体想做什么)是先在一个中间图像上画出椭圆(大小合适,但不旋转),然后使用 image.rotate() 方法旋转它,最后再把它粘贴到你想要的目标图像上。

希望这对你有帮助。

撰写回答