2011-08-24 89 views
0

我想繪製圖像中繪製一個傾斜的橢圓。但是,我不確定如何定義它,因爲雖然下面的方案會移動點,但我認爲這隻會擠壓橢圓,而不是旋轉它(也是我認爲在任何情況下都存在某種程度上的錯誤)。我將該函數的輸出提供給橢圓命令並將其添加到現有圖片,因此任何會旋轉整個圖像的方法都不好。 OD只是我使用的座標中心的平方偏移量。如何在ImageDraw中繪製一個傾斜的橢圓?

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 

有誰知道如何做到這一點?

+0

您應該只需要半大小,半微小,中心點和一個角度來完全描述橢圓。不知道爲什麼你有額外的角度(角度和傾斜度),除非你繪製了一個扁平的橢圓,這實際上並不是一個橢圓。 – John

+0

那,並檢查這個問題。 http://stackoverflow.com/questions/87734/how-do-you-calculate-the-axis-aligned-bounding-box-of-an-ellipse – John

+0

橢圓所在的座標系是極座標。偏移和角度移動橢圓的中心點(相對於PIL座標中的offset = 0(OD,OD)),而主要,次要和傾斜控制着它的形狀。 – Elliot

回答

2

它看起來像是用來繪製橢圓的「盒子」沒有與其相關的旋轉。它只是由(左,上,右,底)範圍定義的。

一種可能的解決方法(取決於您需要做的)是將橢圓(尺寸正確但沒有旋轉)繪製到中間圖像上,使用image.rotate()方法,然後將其粘貼到目標圖像中。

我希望有幫助。

+0

好的一般方法,但如何使用'Image.rotate'而不是滾動。 – tom10

+0

好,我已經更新了我的答案。謝謝。 – tugs