1.按比例缩放
import cv2import osif __name__ == '__main__':img_load_path='./test/img/' #原图片路径img_save_path = './test/img_change/' # 修改大小后保存路径img_names=os.listdir(img_load_path)i=0 #图片命名、计数for img_name in img_names:img_path=img_load_path+img_nameimg = cv2.imread(img_path,1)# cv2.imshow('resize before', img)h,w=img.shape[:2]# 缩放比例k,>1表示放大,<1表示缩小k = 2 / 3# 元组参数,为宽,高kimg = cv2.resize(img, (int(w * k), int(h * k)),interpolation=cv2.INTER_LINEAR)# cv2.imshow('resize after', img)# 写入新文件cv2.imwrite('./test/img_change/'+str(i)+'.png', img)i += 1# 延迟关闭cv2.waitKey()
2.以固定大小截取图片
import cv2import numpy as npimport osdef img_cut(img, side, img_save_path,num):h_size, w_size = img.shape[0], img.shape[1] # 高宽尺寸# 共截取(num_h+1)* (num_w+1)张图片,num_h, num_w = h_size//side, w_size//side # 截取后图片的计数编号num = (num_h+1) * (num_w+1) * num img = np.array(img)img_crop = np.zeros((side,side,3))image = []for h in range(0,num_h):for w in range(0,num_w):img_crop = img[h*side:(h+1)*side,w*side:(w+1)*side]image.append(img_crop)if w == num_w - 1:img_crop = img[h*side:(h+1)*side,w_size-side:w_size]image.append(img_crop)if h == num_h - 1:for w in range(0,num_w):img_crop = img[h_size-side:h_size,w*side:(w+1)*side]image.append(img_crop)if w == num_w - 1:img_crop = img[h_size-side:h_size,w_size-side:w_size]image.append(img_crop)for i in range(0,len(image)):image_i = image[i]path_image_i = img_save_path + str(num+i+1)+str('.jpg')cv2.imwrite(path_image_i,image_i)if __name__ == '__main__':img_load_path='./test/img/' #原图片路径img_save_path = './test/img_change/' # 截取后保存路径side = 1000 # 裁剪大小1000*1000img_names=os.listdir(img_load_path)i=0 #来记录第几张图片for img_name in img_names:img_path=os.path.join(img_load_path,img_name)img = cv2.imread(img_path) img_cut(img ,side=side,img_save_path=img_save_path,num=i)i+=1
效果
原图
截取后的图
如果觉得《python实现图片按比例缩放 以固定大小截取图片》对你有帮助,请点赞、收藏,并留下你的观点哦!