画像に余白をつける

2020/11/25

Python3.8.2, OpenCV4.4.0

top = bottom = left = right = 10
color = (255,255,255)
img = cv2.copyMakeBorder(img,top, bottom, left, right,cv2.BORDER_CONSTANT,value=color)


画像を指定サイズに余白をつけて調整する(元画像が中央にくるように)

import cv2

img = cv2.imread(path)
height,width = img.shape[:2]

target_size = (400,300) #src size < dst sizeの前提

top = int((target_size[1] - height)/2)
bottom = target_size[1] - height - top
left = int((target_size[0] - width)/2)
right = target_size[0] - width - left

color = (255,255,255)
img = cv2.copyMakeBorder(img, top, bottom, left, right,cv2.BORDER_CONSTANT,value=color)

cv2.imwrite(filepath, img)


画像上の基本的な処理 画像の境界領域を作る(パディング)
http://labs.eecs.tottori-u.ac.jp/sd/Member/oyamada/OpenCV/html/py_tutorials/py_core/py_basic_ops/py_basic_ops.html#id6