使用Pillow实现拼接图像衔接处高斯模糊时出现黑色背景问题求助
解决图像拼接衔接处模糊过渡的问题
你的问题出在蒙版使用逻辑错误——直接用带黑色透明区域的蒙版粘贴,会让背景色(默认黑色)透出来,而非让图像本身的衔接区域产生自然模糊。下面是正确的实现方案:
核心思路
- 先完成基础的图像拼接
- 提取衔接处的重叠区域(同时包含左右两张图的边缘部分)
- 单独对这段重叠区域做高斯模糊
- 将模糊后的区域覆盖回原拼接图像的对应位置
完整代码示例
from PIL import Image, ImageFilter # 假设im_composite是你的原始RGBA图像 # 第一步:完成基础拼接 stitched_images = Image.new('RGBA', (im_composite.width * 2, im_composite.height)) stitched_images.paste(im_composite, (0, 0)) stitched_images.paste(im_composite, (im_composite.width, 0)) # 第二步:设置过渡模糊参数(可根据需求调整) blur_width = 10 # 过渡区域的总宽度 blur_radius = 2 # 高斯模糊的半径 # 计算过渡区域的边界 left_bound = im_composite.width - blur_width // 2 right_bound = im_composite.width + blur_width // 2 # 第三步:提取并模糊过渡区域 transition_area = stitched_images.crop((left_bound, 0, right_bound, im_composite.height)) blurred_area = transition_area.filter(ImageFilter.GaussianBlur(radius=blur_radius)) # 第四步:将模糊区域覆盖回拼接图 stitched_images.paste(blurred_area, (left_bound, 0)) # 查看最终效果 stitched_images.show()
错误原因说明
你之前的代码中,蒙版里填充0的区域会让result的黑色背景透出来,本质是通过透明度控制显示背景,而非对图像自身像素做模糊处理。直接对衔接区域的图像像素做模糊,才能得到自然的颜色过渡效果。
内容的提问来源于stack exchange,提问作者Daniel Bertoldi
相关产品推荐
相关产品推荐

