微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

屏蔽两幅图像并合并为一幅图像

如何解决屏蔽两幅图像并合并为一幅图像

我正在做一个项目,我在两张不同的图片上使用不同的蒙版,而不是想将它们组合成一张图片。到目前为止,我有遮罩(尽管边缘有一些错误),现在我正在尝试组合图像。

  1. 如何改进遮罩,使结果在边缘上没有错误(参见图片
  2. 如何有效地将图像合并为一个生成第三个图像?我一直在尝试使用一些透明效果,但没有奏效。我想要做的是合并两个图像,使它们形成一个完整的圆圈。如果需要任何原始图像,请告诉我
    from PIL import Image
    
  
    # load images
    img_day  = Image.open('Day.jpeg')
    img_night  = Image.open('Night_mirror.jpg')
    night_mask = Image.open('Masks/12.5.jpg')
    day_mask = Image.open('Masks/11.5.jpg')
    
    # convert images
    #img_org  = img_org.convert('RGB') # or 'RGBA'
    night_mask = night_mask.convert('L')    # grayscale
    day_mask = day_mask.convert('L')
    
    # the same size
    img_day  = img_day.resize((750,750))
    img_night  = img_night.resize((750,750))
    night_mask = night_mask.resize((750,750))
    day_mask = day_mask.resize((750,750))
    # add alpha channel    
    
    img_day.putalpha(day_mask)
    img_night.putalpha(night_mask)
    img_night = img_night.rotate(-170)
    
    
    # save as png which keeps alpha channel 
    img_day.save('image_day.png')
    img_night.save('image_night.png')
    img_night.show()
    img_day.show()

enter image description here

感谢任何帮助

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

解决方法

给你点:

  1. 您使用遮罩的问题只是源于您的遮罩并不完美。在油漆中打开它们,您会看到顶部有一条白线。只需使用填充工具用黑色填充白色部分。之后它应该可以工作了。

  2. 我建议水平镜像您的图像而不是旋转它。为此,您可以使用 PIL.ImageOps.mirror。然后使用 img.paste() 将一个图像粘贴到另一个图像上。作为第二个参数,您给出图像应该粘贴到另一个的坐标,非常重要的是,作为第三个参数,您指定一个透明蒙版。由于您的图像已经具有 Alpha 通道,因此您可以使用相同的图像作为蒙版。 PIL 将自动使用它的 alpha 通道进行屏蔽。请注意,我必须将粘贴位置调整 4 个像素才能正确重叠图像。

from PIL import Image,ImageOps

# load images
img_day  = Image.open('day.jpg')
img_night  = Image.open('night.jpg')
night_mask = Image.open('night_mask.jpg')
day_mask = Image.open('day_mask.jpg')

# convert images
#img_org  = img_org.convert('RGB') # or 'RGBA'
night_mask = night_mask.convert('L')    # grayscale
day_mask = day_mask.convert('L')

# the same size
img_day  = img_day.resize((750,750))
img_night  = img_night.resize((750,750))
night_mask = night_mask.resize((750,750))
day_mask = day_mask.resize((750,750))
# add alpha channel    

img_day.putalpha(day_mask)
img_night.putalpha(night_mask)
img_night = ImageOps.mirror(img_night)


img_night.paste(img_day,(-4,0),img_day)
img_night.save('composite.png')

结果:

enter image description here

,

主要问题是蒙版中的 (JPG) 伪影(顶部的白线,“平滑”边缘)。为什么不使用 ImageDraw.arc 即时生成掩码?您需要的最后一步是使用 Image.composite 合并您的两个图像。

这是一些代码(我将您的第一张图像作为所需的输出,因此选择了角度):

from PIL import Image,ImageDraw

# Load images
img_day = Image.open('day.jpg')
img_night = Image.open('night.jpg')

# Resize images
target_size = (750,750)
img_day = img_day.resize(target_size)
img_night = img_night.resize(target_size)

# Generate proper masks
day_mask = Image.new('L',target_size)
draw = ImageDraw.Draw(day_mask)
draw.arc([10,10,740,740],120,270,255,150)
night_mask = Image.new('L',target_size)
draw = ImageDraw.Draw(night_mask)
draw.arc([10,150)

# Put alpha channels
img_day.putalpha(day_mask)
img_night.putalpha(night_mask)

# Compose and save image
img = Image.composite(img_day,img_night,day_mask)
img.save('img.png')

这就是输出:

Output

----------------------------------------
System information
----------------------------------------
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.5
Pillow:      8.0.1
----------------------------------------

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。