python numpy 2b opencv 2b overlay image

Solutions on MaxInterview for python numpy 2b opencv 2b overlay image by the best coders in the world

showing results for - "python numpy 2b opencv 2b overlay image"
Federico
28 Nov 2019
1import cv2
2
3background = cv2.imread('field.jpg')
4overlay = cv2.imread('dice.png')
5
6added_image = cv2.addWeighted(background,0.4,overlay,0.1,0)
7
8cv2.imwrite('combined.png', added_image)
9
Javier
29 Apr 2018
1import cv2
2import numpy as np
3
4def overlay_transparent(background, overlay, x, y):
5
6    background_width = background.shape[1]
7    background_height = background.shape[0]
8
9    if x >= background_width or y >= background_height:
10        return background
11
12    h, w = overlay.shape[0], overlay.shape[1]
13
14    if x + w > background_width:
15        w = background_width - x
16        overlay = overlay[:, :w]
17
18    if y + h > background_height:
19        h = background_height - y
20        overlay = overlay[:h]
21
22    if overlay.shape[2] < 4:
23        overlay = np.concatenate(
24            [
25                overlay,
26                np.ones((overlay.shape[0], overlay.shape[1], 1), dtype = overlay.dtype) * 255
27            ],
28            axis = 2,
29        )
30
31    overlay_image = overlay[..., :3]
32    mask = overlay[..., 3:] / 255.0
33
34    background[y:y+h, x:x+w] = (1.0 - mask) * background[y:y+h, x:x+w] + mask * overlay_image
35
36    return background
37