python - 使用PIL的粘贴方法时的透明度问题

标签 python image rotation python-imaging-library

由于 PIL 似乎无法以一定角度创建椭圆,因此我编写了一个函数来执行此操作。问题是,当我使用 PIL 粘贴时,它似乎没有使用 alpha 信息。有谁知道如果所有图像都是 RGBA,为什么透明度信息会被丢弃?

提前致谢!:

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

最佳答案

尝试在粘贴时添加掩码,如下所示:

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

关于python - 使用PIL的粘贴方法时的透明度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44159114/

相关文章:

python - 在迭代时将字符大写,就像 python 中的选取框

python - 从 Python 调用静态 C 库

windows - 当我们在 Windows 中使用 docker 镜像和容器时,它存储在哪里?

objective-c - iOS:从非图像数据生成图像(Godus like landscape)

c++ - 如何根据物体的方向旋转物体

c++ - 在 OpenGL C++ 中旋转子对象

python - 使用散点数据集生成热图

python - 似乎 pyodb 已安装,但出现错误,提示找不到模块 python 3.7.5。 Jupyter实验室

python - 测量图像中文本边框的直/平滑度

java - 无法正确旋转 3D 点 A 到 B(在 X、Y、Z 轴上)