【问题标题】:How to lower transparency to line in Pillow?如何降低透明度以在 Pillow 中排列?
【发布时间】:2021-11-05 18:32:12
【问题描述】:

如何降低线条的不透明度?我想将不透明度降低到下面示例中的一行。

from PIL import Image, ImageDraw

img = Image.new('RGB', (100, 100), (255, 255, 255))
draw = ImageDraw.Draw(img)
draw.line((100, 30, 0, 30), (0, 0, 0), 20)
draw.line((100, 70, 0, 70), (0, 0, 0), 20)
img.show()

我在一个例子中看到他们创建了这样的不透明度......

TRANSPARENCY = .25  # Degree of transparency, 0-100%
OPACITY = int(255 * TRANSPARENCY)

但不知道如何应用于其中一行。有什么想法吗?

编辑

我做了一些更改(基于@Pedro Maia 的回答),它仍然不起作用,只是更改了颜色,它不会降低不透明度以查看背景颜色。

from PIL import Image, ImageDraw

img = Image.new('RGBA', (500, 500), (255, 255, 255))
draw = ImageDraw.Draw(img)
TRANSPARENCY = .25  # Degree of transparency, 0-100%
draw.line((200, 0, 200, 600),(255, 0, 0), 60)
draw.line((500, 100, 0, 100), (0, 0, 0, int(255 * TRANSPARENCY)), 60)
draw.line((500, 400, 0, 400),(0, 0, 0), 60)
img

我必须将其转换为 RGB 以将其导出为“jpg”

【问题讨论】:

    标签: python python-imaging-library


    【解决方案1】:

    你必须做这样的事情,这类似于example code 的工作方式,才能做(我认为)你想做的事情。我稍微更改了您在 EDIT 中添加到问题中的代码,以便更好地展示可以绘制不同透明度的线条。

    from PIL import Image, ImageDraw
    
    RED = (255, 0, 0)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    
    # Calculate alpha given a 0-100% opacity value.
    opacity = lambda transparency: (int(255 * (transparency/100.)),)  # Returns a monuple.
    
    def draw_transp_line(image, xy, color, width=1, joint=None):
        """ Draw line with transparent color on the specified image. """
        if len(color) < 4:  # Missing alpha?
            color += opacity(100)  # Opaque since alpha wasn't specified.
    
        # Make an overlay image the same size as the specified image, initialized to
        # a fully transparent (0% opaque) version of the line color, then draw a
        # semi-transparent line on it.
        overlay = Image.new('RGBA', image.size, color[:3]+opacity(0))
        draw = ImageDraw.Draw(overlay)  # Create a context for drawing things on it.
        draw.line(xy, color, width, joint)
        # Alpha composite the overlay image onto the original.
        image.alpha_composite(overlay)
    
    
    # Create opaque white RGBA background image.
    img = Image.new('RGBA', (500, 500), (255, 255, 255)+opacity(100))
    
    draw_transp_line(img, ((200, 0), (200, 600)), RED+opacity(100), 60)
    draw_transp_line(img, ((500, 100), (0, 100)), BLACK+opacity(25), 60)
    draw_transp_line(img, ((150, 50), (600, 400)), BLACK+opacity(50), 60)
    
    img = img.convert("RGB") # Remove alpha for saving in jpg format.
    img.save('transparent_lines.jpg')
    img.show()
    

    已创建 JPG 图片:

    【讨论】:

      【解决方案2】:

      使用 draw.line,您可以将 RGBRGBA 作为参数传递,只需传递透明度的值即可:

      draw.line((100, 30, 0, 30), (0, 0, 0, int(255 * TRANSPARENCY)), 20)
      

      在创建图像时也将其设置为RGBA:

      img = Image.new('RGBA', (100, 100), (255, 255, 255))
      

      【讨论】:

      • 没什么变化,可以提供图片吗?
      • 您还必须将图像模式设置为RGBA
      • 它只是改变颜色,它不会让你从一种颜色中看到背景颜色。请查看更新后的问题。
      猜你喜欢
      • 1970-01-01
      • 2023-02-07
      • 1970-01-01
      • 2014-05-31
      • 2020-05-04
      • 1970-01-01
      • 2018-01-31
      • 2014-08-31
      • 1970-01-01
      相关资源
      最近更新 更多