【问题标题】:PIL Drawing a semi-transparent square overlay on imagePIL 在图像上绘制半透明方形覆盖
【发布时间】:2017-09-22 22:54:09
【问题描述】:
from PIL import Image
from PIL import ImageDraw 
from io import BytesIO
from urllib.request import urlopen

url = "https://i.ytimg.com/vi/W4qijIdAPZA/maxresdefault.jpg"
file = BytesIO(urlopen(url).read())
img = Image.open(file)
img = img.convert("RGBA")
draw = ImageDraw.Draw(img, "RGBA")
draw.rectangle(((0, 00), (img.size[0], img.size[1])), fill=(0,0,0,127))
img.save('dark-cat.jpg')

这给了我一个巨大的黑色方块。我希望它是一个带有猫的半透明黑色正方形。有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    抱歉,我关于它是错误的评论不正确,所以...

    您可以通过创建一个临时图像并使用Image.alpha_composite() 来做到这一点,如下面的代码所示。请注意,它支持黑色以外的半透明方块。

    from PIL import Image, ImageDraw
    from io import BytesIO
    from urllib.request import urlopen
    
    TINT_COLOR = (0, 0, 0)  # Black
    TRANSPARENCY = .25  # Degree of transparency, 0-100%
    OPACITY = int(255 * TRANSPARENCY)
    
    url = "https://i.ytimg.com/vi/W4qijIdAPZA/maxresdefault.jpg"
    with BytesIO(urlopen(url).read()) as file:
        img = Image.open(file)
        img = img.convert("RGBA")
    
    # Determine extent of the largest possible square centered on the image.
    # and the image's shorter dimension.
    if img.size[0] > img.size[1]:
        shorter = img.size[1]
        llx, lly = (img.size[0]-img.size[1]) // 2 , 0
    else:
        shorter = img.size[0]
        llx, lly = 0, (img.size[1]-img.size[0]) // 2
    
    # Calculate upper point + 1 because second point needs to be just outside the
    # drawn rectangle when drawing rectangles.
    urx, ury = llx+shorter+1, lly+shorter+1
    
    # Make a blank image the same size as the image for the rectangle, initialized
    # to a fully transparent (0% opaque) version of the tint color, then draw a
    # semi-transparent version of the square on it.
    overlay = Image.new('RGBA', img.size, TINT_COLOR+(0,))
    draw = ImageDraw.Draw(overlay)  # Create a context for drawing things on it.
    draw.rectangle(((llx, lly), (urx, ury)), fill=TINT_COLOR+(OPACITY,))
    
    # Alpha composite these two images together to obtain the desired result.
    img = Image.alpha_composite(img, overlay)
    img = img.convert("RGB") # Remove alpha for saving in jpg format.
    img.save('dark-cat.jpg')
    img.show()
    

    以下是将其应用于您的测试图像的结果:

    【讨论】:

    • 感谢您的帮助。我刚刚创建了一个新问题:github.com/python-pillow/Pillow/issues/2496
    • @ChaseRoberts 这不是错误,而是对 Draw 应该做什么的误解。它不进行混合,它用一组新像素替换一组像素。 example in the documentation 表明这是一个两步过程,在空白画布上使用不透明度进行绘制,然后合成结果。如本答案的第二部分所示。
    • @MarkRansom:确实,所以我的解决方法是 方法(一般而言)。
    • @MinhNghĩa:诚然,文档在这个主题上非常简洁。也许alpha compositing 上的维基百科文章会帮助您了解它的作用。
    • @MinhNghĩa 那么我们有 StackOverflow 是件好事!可能 ImageDraw 的作者认为它是如此不言而喻,无需解释。
    【解决方案2】:

    如果你只是想让整个图像变暗,有一个更简单的方法:

    img = Image.eval(img, lambda x: x/2)
    

    【讨论】:

      【解决方案3】:

      鉴于每当我想用 PIL 绘制一个透明矩形时,我都会不断地回到这个问题上,所以我决定进行更新。

      如果我只更改一件事,您的代码对我来说非常有用:将图像保存为 PNG 格式而不是 JPEG。

      所以当我跑步时

      from io import BytesIO
      from urllib.request import urlopen
      from PIL import Image
      from PIL import ImageDraw
      
      url = "https://i.ytimg.com/vi/W4qijIdAPZA/maxresdefault.jpg"
      file = BytesIO(urlopen(url).read())
      img = Image.open(file)
      draw = ImageDraw.Draw(img, "RGBA")
      draw.rectangle(((280, 10), (1010, 706)), fill=(200, 100, 0, 127))
      draw.rectangle(((280, 10), (1010, 706)), outline=(0, 0, 0, 127), width=3)
      img.save('orange-cat.png')
      

      我得到了这个美妙的图像:

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多