【问题标题】:Transparency issues drawing a rectangle with rounded corners绘制圆角矩形的透明度问题
【发布时间】:2018-02-14 10:13:42
【问题描述】:

我正在尝试使用我在教程中找到的一些代码绘制圆角矩形,并由我稍作修改:

# Rounded rectangle algorithm copied from http://ju.outofmemory.cn/entry/18060
def round_corner(self, radius, fill):
    corner = Image.new('RGBA', (radius, radius), (0, 0, 0, 0))
    draw = ImageDraw.Draw(corner)
    draw.pieslice((0, 0, radius * 2, radius * 2), 180, 270, fill=(fill))
    return corner

def round_rectangle(self, size, radius, fill):
    width, height = size
    rectangle = Image.new('RGBA', size, red)
    corner = self.round_corner(radius, fill)
    rectangle.paste(corner, (0, 0))
    rectangle.paste(corner.rotate(90), (0, height - radius)) # Rotate the corner and paste it
    rectangle.paste(corner.rotate(180), (width - radius, height - radius))
    rectangle.paste(corner.rotate(270), (width - radius, 0))
    return rectangle

    # Get rounded box
    img = self.round_rectangle((200, 200), 30, black)
    # Join with output image
    self.image_canvas.paste(img, (500,500))     

但是用 tkinter 显示后我的结果是这样的:

注意圆角之外的灰色方角。这似乎发生在我的 Windows 和 Ubuntu 开发机器上。我不确定他们是如何到达那里或如何摆脱他们的。

【问题讨论】:

    标签: python python-3.x python-imaging-library pillow


    【解决方案1】:

    事实证明,即使原始图像本身具有 Alpha 通道,粘贴功能也需要蒙版。因此,您可以直接使用要合并的图像的 Alpha 通道作为蒙版:

     self.image_canvas.paste(img, (500,500), img)
    

    【讨论】:

    • 如果这是适合您的解决方案,您可以接受自己的答案
    猜你喜欢
    • 2016-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多