2017-05-24 1 views
2

PIL은 어떤 각도에서 타원을 만들 수있는 것 같지 않으므로이 작업을 수행하는 함수를 작성했습니다. 문제는 PIL을 사용하여 붙여 넣을 때 알파 정보를 사용하지 않는 것입니다. 왜 모든 이미지가 RGBA 인 경우 투명 정보가 삭제되는지 알 수 있습니까? 사전! :PIL의 붙여 넣기 방법을 사용하는 동안 투명도 문제가 발생했습니다.

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 

에서

덕분에 나는 다음과 같이 그것을 호출하려고하면 :

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() 

을 나는이 얻을 :

output image

답변

3

시도해보십시오 mask 경우 추가를 붙여 넣기 :

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

예 : 당신에게 뭔가를 제공해야

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() 

는 :

ellipses

+1

나는 너무 가까이했다! 그게 효과가있어 - 고마워! –

관련 문제