1.环境
ubuntu16.04
python3.6
opencv
2.代码
import cv2
def reduced_fraction(n, m):
n=int(n)
m=int(m)
for i in range(2,n):
while(n%i==0 and m%i==0):
n=n//i
m=m//i
return n,m
img = cv2.imread("1.jpg")
h, w, c = img.shape
crop_factor = 1
h_i, w_i = reduced_fraction(h, w)
# center crop : 裁剪了2*h_i*crop_factor与2*w_i*crop_factor的像素。
crop_img = img[h_i*crop_factor:h-h_i*crop_factor, w_i*crop_factor:w-w_i*crop_factor]
img = cv2.resize(crop_img, (w, h))