Gau Rav
Gau Rav

Reputation: 355

crop image and paste in same coords as from source

I am able to detect the person and able to find the coordinates of the person and then i can crop the image. but i want to keep the person and remove all the background/ make it white background / copy the cropped image and paste it in another file in the same coordinates as obtained from the source. enter image description here

This is my required image:

enter image description here

Upvotes: 2

Views: 736

Answers (1)

yakhyo
yakhyo

Reputation: 1656

  1. Make a white background image using height and width of input image.
  2. Place the cropped image into white background image according to the coordinates.

img1

import cv2
import numpy as np

image = cv2.imread('img.png')
person = image[110:532, 250:516] # persons coordinates

h, w, c = image.shape

white_background = np.zeros([h, w, 3])
for y in range(h):
    for x in range(w):
        white_background[y,x] = [255,255,255] # fill with white pixels

white_background[110:532, 250:516] = person  # paste the image into background image

cv2.imwrite('resized_centered.png', white_background)

Result: img2

Upvotes: 1

Related Questions