简体   繁体   中英

How increase an image to a specific size by adding white transparent pixels in python (preferably using opencv)

for example: original image's dimesion = 1080x720x4(4 as in blue, green, red and alpha channel) required dimension = 1920x1080x4 I want White transparent pixels (255,255,255,0) to be added in the border until it reaches the required dimension.

Maybe there is numpy function to help with this?

One way using Python/OpenCV is to use copyMakeBorder as suggested by @ Quang Hoang.

Input (100x116):

在此处输入图像描述

import cv2

# read image
img = cv2.imread("A.png", cv2.IMREAD_UNCHANGED)

img2 = cv2.copyMakeBorder(img, 50,50,50,50, borderType=cv2.BORDER_CONSTANT, value=(255,255,255,0))

cv2.imwrite("A_border.png", img2)

cv2.imshow("img2", img2)
cv2.waitKey(0)

Result (200x2016):

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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