0

I am trying to draw a tilted ellipse in image draw. However, I am not sure how to define it, since while the scheme below would move the points, I think this would just squish the ellipse, not rotate it (also I think there is something slightly wrong with the transformation in any case). I am feeding the output of this function into the ellipse command and adding it to an existing picture, so any methods that would rotate the entire image are no good. OD is just a square offset to the coordinate center I am using.

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

Does anyone know how to accomplish this?

Elliot
  • 5,211
  • 10
  • 42
  • 70
  • You should only need semimajor, semiminor, center point, and one angle to describe ellipse completely. Not sure why you have the additional angle (both `angle` and `tilt`) unless you're plotting a squished ellipse, which isn't really an ellipse per se. – John Aug 24 '11 at 16:07
  • That, and check this question. http://stackoverflow.com/questions/87734/how-do-you-calculate-the-axis-aligned-bounding-box-of-an-ellipse – John Aug 24 '11 at 16:11
  • The coordinate system in which the ellipse is being placed is polar. Offset and angle move the center point of the ellipse (relative to offset=0 being (OD,OD) in PIL coordinates), while major, minor, and tilt control the shape of it. – Elliot Aug 24 '11 at 16:13
  • How does this solve the problem of rotation? As I re-read the specification, the bounding box will always be a straight rectangle (you are only specifying 2 corners, so there is no angular information) and the ellipse will be the one that takes the maximum area (eg the un-tilted ellipse). There is something going on here that I do not understand. – Elliot Aug 24 '11 at 16:32
  • Is this what you're using? http://www.pythonware.com/library/pil/handbook/imagedraw.htm If so, it doesn't look like you can do much with that. – John Aug 24 '11 at 16:46
  • Yes, that is what I am using. – Elliot Aug 24 '11 at 17:32

1 Answers1

3

It looks like the 'box' that is being used to draw the ellipse has no rotation associated with it. It is simply defined by the (left, top, right, bottom) extents.

One possible workaround (depending on what you need to do) is to draw the ellipse (sized correctly, but without the rotation) onto an intermediary image, use the image.rotate() method, and then paste it into your target image.

I hope that helps.

tugs
  • 583
  • 3
  • 18
  • Good general approach, but how about using `Image.rotate` instead of rolling. – tom10 Sep 10 '11 at 16:07
  • This suggestion here from @tugs was the best way I found to add an ellipse at an angle to a drawing. I found this answer https://stackoverflow.com/a/34748617/559525 and it was a great approach to draw a rotated bounding box, but it could not be used to then draw the ellipse. – Eradicatore Aug 28 '23 at 13:48