【问题标题】:Pillow - Transparency over non-transparent image with paste枕头 - 使用粘贴在不透明图像上的透明度
【发布时间】:2020-05-29 19:18:20
【问题描述】:

让我先声明一下,我对图像/图形一无所知,所以也许我对这里的某些东西缺乏基本的了解。

我正在尝试将图像 (game_image) 粘贴到我的基本图像 (image) 上,并在顶部添加一个透明覆盖 (overlay_image) 来为文本添加一些变暗。

以下是预期结果的示例:

这是我当前代码生成的示例:

这是我当前的代码:

from PIL import Image, ImageFont, ImageDraw

# base image sizing specific to Twitter recommended
base_image_size = (1600, 900)
base_image_mode = "RGBA"
base_image_background_color = (0, 52, 66)

image = Image.new(base_image_mode, base_image_size, base_image_background_color)

# game_image is the box art image on the left side of the card
game_image = Image.open("hunt.jpg")
image.paste(game_image)

# overlay_image is the darkened overlay over the left side of the card
overlay_image = Image.new(base_image_mode, base_image_size, (0, 0, 0))
overlay_image.putalpha(128)

# x position should be negative 50% of base canvas size
image.paste(overlay_image, (-800, 0), overlay_image)
image.save("test_image.png", format="PNG")

您可以看到游戏图像在某种程度上继承了叠加层的透明度。我怀疑这与我在上面的粘贴中添加的mask 有关,但我尝试研究掩蔽是什么,并且在我发现它的任何上下文中都超出了我的理解范围。

感谢任何帮助理解为什么会发生这种情况和/或我如何解决的问题!

【问题讨论】:

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


    【解决方案1】:

    您非常接近...您所需要的只是使用Image.alpha_composite 而不是paste。所以,你的代码的最后两行应该是:

    image = Image.alpha_composite(image, overlay_image)
    image.save("test_image.png", format="PNG")
    

    【讨论】:

    • 工作完美,我会阅读相关文档以尝试了解其中的差异,非常感谢!
    猜你喜欢
    • 2020-03-28
    • 2017-06-04
    • 2021-06-05
    • 2019-05-18
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 2012-12-01
    • 2013-07-18
    相关资源
    最近更新 更多