缩放、裁剪和补边 — OpenCV& Python

import cv2

img_path = 'C:/Users/WZChan/Desktop/'
img = cv2.imread(img_path + 'test_600x350.jpg')

#resize img to 300x200
img_300x200 = cv2.resize(img, (200, 300))
#cv2.resize(imgfile, (width, height))

img_half = cv2.resize(img, (0, 0), fx = 0.5, fy = 0.5, interpolation = cv2.INTER_NEAREST)

img_300x300 = cv2.copyMakeBorder(img, 50, 50, 0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0))

patch = img[20:150, -180:-50]
# NOTE: its img[y: y + h, x: x + w] and *not* img[x: x + w, y: y + h]

cv2.imwrite(img_path + 'cropped_img.jpg', patch)
cv2.imwrite(img_path + 'resized_300x200.jpg', img_300x200)
cv2.imwrite(img_path + 'resized_half.jpg', img_half)
cv2.imwrite(img_path + 'resized_300x300.jpg', img_300x300)
缩放、裁剪和补边 — OpenCV& Python_第1张图片
cropped_img.jpg
缩放、裁剪和补边 — OpenCV& Python_第2张图片
resized_300x200.jpg
缩放、裁剪和补边 — OpenCV& Python_第3张图片
resized_half.jpg
缩放、裁剪和补边 — OpenCV& Python_第4张图片
resized_300x300.jpg

你可能感兴趣的:(缩放、裁剪和补边 — OpenCV& Python)