简体   繁体   English

如何合并两个图像基础透明python

[英]How to merge two images base transparent python

I need to join two images where the base image has a transparent background, I already tried to do it using我需要加入两个基本图像具有透明背景的图像,我已经尝试过使用

image 01图片 01

在此处输入图像描述

image and a photo I need to put her in the second in the blank space second image图片和一张照片我需要把她放在第二张空白处第二张图片

在此处输入图像描述

expected result and this预期结果和这个

在此处输入图像描述

import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

dim = (425, 425)

apple = mpimg.imread('image.png')
apple = cv2.resize(apple, dim)

banana = mpimg.imread('image2.png')
banana = cv2.resize(banana, dim)


_ = plt.imshow(apple)
_ = plt.show()

_ = plt.imshow(banana)
_ = plt.show()

list_images = [apple, banana]

def blend(list_images): # Blend images equally.

    equal_fraction = 1.0 / (len(list_images))

    output = np.zeros_like(list_images[0])

    for img in list_images:
        output = output + img * equal_fraction

    output = output.astype(np.uint8)
    _ = plt.imshow(output)
    return output


output = blend(list_images)

_ = plt.imshow(output)

You can easily make use of your alpha (transparent) channel for this purpose.为此,您可以轻松地使用 Alpha(透明)通道。 Unfortunately, when I tried reading your frame image using cv2.IMREAD_UNCHANGED , it didn't have the appropriate alpha channel.不幸的是,当我尝试使用cv2.IMREAD_UNCHANGED读取您的帧图像时,它没有适当的 alpha 通道。 According to your result, the region outside the rounded corners is in white.根据您的结果,圆角以外的区域是白色的。

So using your frame image I created the alpha channel and used it the following.因此,使用您的帧图像,我创建了 alpha 通道并使用它如下。

# Reading images
tr = cv2.imread('frame.png')
la = cv2.imread('sunset.jpg')

# resizing
dim = (425, 425)
tr = cv2.resize(tr, dim)
la = cv2.resize(la, dim)

# Finding the largest contour (white region) from the first channel in frame
th = cv2.threshold(tr[:,:,0],127,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
contours, hierarchy = cv2.findContours(th, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
c = max(contours, key = cv2.contourArea)

# Draw the largest contour onto a mask
black = np.zeros((tr.shape[0], tr.shape[1]), np.uint8)
mask = cv2.drawContours(black,[c],0,255, -1)

Mask image: we want the sunset image to be present in the white region蒙版图像:我们希望日落图像出现在白色区域

在此处输入图像描述

# Create 3-channel mask of float datatype
alpha = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)/255.0

# Perform blending and limit pixel values to 0-255
blended = cv2.convertScaleAbs(tr*(1-alpha) + la*alpha)

在此处输入图像描述

Here is the minimalistic code needed to apply the mask:这是应用掩码所需的简约代码:

import cv2

img1 = cv2.imread("img1.jpg")
img2 = cv2.imread("img2.png", cv2.IMREAD_GRAYSCALE)

img1 = cv2.resize(img1, img2.shape[1::-1])
img1[img2 < 128] = 0

cv2.imshow("Image", img1)
cv2.waitKey(0)

Input images img1.jpg and img2.png :输入图像img1.jpgimg2.png

在此处输入图像描述

在此处输入图像描述

Output:输出:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM