【python图像处理】两幅图像的合成一幅图像(blending two images)
生活随笔
收集整理的這篇文章主要介紹了
【python图像处理】两幅图像的合成一幅图像(blending two images)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
將兩幅圖像合成一幅圖像,是圖像處理中常用的一種操作,python圖像處理庫(kù)PIL中提供了多種種將兩幅圖像合成一幅圖像的接口。
下面我們通過(guò)不同的方式,將兩圖合并成一幅圖像。
?
1、使用Image.blend()接口
代碼如下:
from PIL import Imagedef blend_two_images():img1 = Image.open( "bridge.png ")img1 = img1.convert('RGBA')img2 = Image.open( "birds.png ")img2 = img2.convert('RGBA')img = Image.blend(img1, img2, 0.3)img.show()img.save( "blend.png")return
兩幅圖像進(jìn)行合并時(shí),按公式:blended_img = img1 * (1 – alpha) + img2* alpha 進(jìn)行。
合成結(jié)果如下:
2、使用Image.composite()接口
該接口使用掩碼(mask)的形式對(duì)兩幅圖像進(jìn)行合并。
代碼如下:
def blend_two_images2():img1 = Image.open( "bridge.png ")img1 = img1.convert('RGBA')img2 = Image.open( "birds.png ")img2 = img2.convert('RGBA')r, g, b, alpha = img2.split()alpha = alpha.point(lambda i: i>0 and 204)img = Image.composite(img2, img1, alpha)img.show()img.save( "blend2.png")return
代碼第9行中指定的204起到的效果和使用blend()接口時(shí)的0.3類(lèi)似。
合并后的效果如下:
2017.05.09
總結(jié)
以上是生活随笔為你收集整理的【python图像处理】两幅图像的合成一幅图像(blending two images)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【python图像处理】图像的滤波(Im
- 下一篇: 【C++】读取文件夹下所有文件名