Home > database >  Resizing image without distortion creates a square background but in black
Resizing image without distortion creates a square background but in black

Time:04-23

In order to maintain the aspect ratio of my image I'm making use of the following code to create a square block and applying my image over this which is fine but the problem is the leftover background is dark/black is there a way to keep that white/transparent?

my code:

def resize_image(img, size=(28,28)):

h, w = img.shape[:2]
c = img.shape[2] if len(img.shape)>2 else 1

if h == w: 
    return cv2.resize(img, size, cv2.INTER_AREA)

dif = h if h > w else w

interpolation = cv2.INTER_AREA if dif > (size[0] size[1])//2 else 
                cv2.INTER_CUBIC

x_pos = (dif - w)//2
y_pos = (dif - h)//2

if len(img.shape) == 2:
    mask = np.zeros((dif, dif), dtype=img.dtype)
    mask[y_pos:y_pos h, x_pos:x_pos w] = img[:h, :w]
else:
    mask = np.zeros((dif, dif, c), dtype=img.dtype)
    mask[y_pos:y_pos h, x_pos:x_pos w, :] = img[:h, :w, :]

return cv2.resize(mask, size, interpolation)

enter image description here

  • Related