2017-05-24 1 views
2

Da PIL nicht in der Lage ist, eine Ellipse in einem Winkel zu erzeugen, schrieb ich eine Funktion, um dies zu tun. Das Problem ist, wenn ich mit PIL einfügen, scheint es nicht die Alpha-Informationen zu verwenden. Weiß jemand, warum die Transparenzinformationen gelöscht werden, wenn alle Bilder RGBA sind?Transparenzproblem bei der Verwendung der PIL-Paste-Methode

Vielen Dank im Voraus !:

from PIL import Image, ImageDraw 

def ellipse_with_angle(im,x,y,major,minor,angle,color): 
    # take an existing image and plot an ellipse centered at (x,y) with a 
    # defined angle of rotation and major and minor axes. 
    # center the image so that (x,y) is at the center of the ellipse 
    x -= int(major/2) 
    y -= int(major/2) 

    # create a new image in which to draw the ellipse 
    im_ellipse = Image.new('RGBA', (major,major), (255,255,255,0)) 
    draw_ellipse = ImageDraw.Draw(im_ellipse, "RGBA") 

    # draw the ellipse 
    ellipse_box = (0,int(major/2-minor/2),major,int(major/2-minor/2)+minor) 
    draw_ellipse.ellipse(ellipse_box, fill=color) 

    # rotate the new image 
    rotated = im_ellipse.rotate(angle) 
    rx,ry = rotated.size 

    # paste it into the existing image and return the result 
    im.paste(rotated, (x,y,x+rx,y+ry))  
    return im 

Wenn ich versuche, dann ist es wie so nennen:

im = Image.new('RGBA', (500,500), (40,40,40,255)) 
im = ellipse_with_angle(im,x=250,y=250,major=300,minor=200,angle=60,color=(255,0,0,255)) 
im = ellipse_with_angle(im,x=300,y=200,major=100,minor=75,angle=130,color=(255,0,255,150)) 
im = make_color_transparent(im,(0,0,0,255)) 
im.show() 

ich diese bekommen:

output image

Antwort

3

Versuchen Sie, eine mask hinzuzufügen, wenn Einfügen wie folgt:

im.paste(rotated, (x,y,x+rx,y+ry), mask=rotated) 

z.B.

from PIL import Image, ImageDraw 

def ellipse_with_angle(im,x,y,major,minor,angle,color): 
    # take an existing image and plot an ellipse centered at (x,y) with a 
    # defined angle of rotation and major and minor axes. 
    # center the image so that (x,y) is at the center of the ellipse 
    x -= int(major/2) 
    y -= int(major/2) 

    # create a new image in which to draw the ellipse 
    im_ellipse = Image.new('RGBA', (major,major), (255,255,255,0)) 
    draw_ellipse = ImageDraw.Draw(im_ellipse, "RGBA") 

    # draw the ellipse 
    ellipse_box = (0,int(major/2-minor/2),major,int(major/2-minor/2)+minor) 
    draw_ellipse.ellipse(ellipse_box, fill=color) 

    # rotate the new image 
    rotated = im_ellipse.rotate(angle) 
    rx,ry = rotated.size 

    # paste it into the existing image and return the result 
    im.paste(rotated, (x,y,x+rx,y+ry), mask=rotated)  
    return im 


im = Image.new('RGBA', (500,500), (40,40,40,255)) 
im = ellipse_with_angle(im,x=250,y=250,major=300,minor=200,angle=60,color=(255,0,0,255)) 
im = ellipse_with_angle(im,x=300,y=200,major=100,minor=75,angle=130,color=(255,0,255,150)) 
#im = make_color_transparent(im,(0,0,0,255)) 
im.show() 

Welche Sie so etwas wie geben sollte:

ellipses

+1

Ich war so nah! Das funktioniert - vielen Dank! –

Verwandte Themen