如何使用 alpha_composite 合成图像,但设置遮罩的不透明度?

时间:2021-07-21 20:15:39

标签: python image-processing python-imaging-library

我附上了三张不同的图像,我想创建一个合成图,显示带有 0.7 alpha 雷达叠加层的 BaseMap 图像,否则透明,但蓝色部分是 0.7 alpha ,以及最后一张 0.7 alpha 图像。

这些是示例图像,但我必须遍历无限量的图像,并将它们堆叠在 BaseMap 上,始终使其可见。我尝试过 alpha_composite,但我无法获得蒙版的 alpha,混合和粘贴将雷达的透明部分和其他图像呈现为白色。

有人可以帮助创建一个函数,该函数可以传入完整的图像列表和 BaseMap,并将它们分层在一起,就像我提到的那样?

RDPS Radar BaseMap

1 个答案:

答案 0 :(得分:1)

我将为我在评论中提到的两个选项提供一些代码:

  • 选项 A:将雷达图像完全融合到雷达背景图像上。将该合成与 alpha 0.7 混合到基础图像上。

  • 选项 B:将带有 alpha 0.7 的雷达背景图像混合到基础图像上。将带有 alpha 0.7 的雷达图像混合到该合成图像上。

要生成 alpha 0.7,您可以简单地将 point operationsImage.getchannelImage.putalpha 结合使用。通过这样做,您以后可以按原样使用 Image.alpha_composite

from PIL import Image

# Read images
basemap = Image.open('basemap.png').convert('RGBA')
radar_bg = Image.open('radar_bg.png').convert('RGBA')
radar = Image.open('radar.png').convert('RGBA')

# Option A: Fully blend radar image onto the radar background image.
# Blend that composite with alpha 0.7 onto the base image.
comp_a = Image.alpha_composite(radar_bg, radar)
comp_a.putalpha(comp_a.getchannel('A').point(lambda x: x * 0.7))
comp_a = Image.alpha_composite(basemap, comp_a)
comp_a.save('comp_a.png')

# Option B: Blend radar background image with alpha 0.7 onto the base
# image. Blend radar image with alpha 0.7 onto that composite.
radar_bg.putalpha(radar_bg.getchannel('A').point(lambda x: x * 0.7))
radar.putalpha(radar.getchannel('A').point(lambda x: x * 0.7))
comp_b = Image.alpha_composite(Image.alpha_composite(basemap, radar_bg), radar)
comp_b.save('comp_b.png')

这是选项 A 的组合:

Option A

而且,这是选项 B 的组合:

Option B

----------------------------------------
System information
----------------------------------------
Platform:      Windows-10-10.0.19041-SP0
Python:        3.9.1
PyCharm:       2021.1.3
Pillow:        8.3.1
----------------------------------------