import cv2
import os
def scale(pic_original, fx, fy):
pic_result = cv2.resize(pic_original, None, fx=fx, fy=fy, interpolation=cv2.INTER_LINEAR)
return pic_result
def main():
fx = 0.5
fy = 0.5
dir_new = r"D:\train\img\image_train5" #缩放后图片存放的位置
if not os.path.exists(dir_new):
os.makedirs(dir_new)
dir_old = r"D:\train\img\image_train" #原始图片文件夹位置
img_list = os.listdir(dir_old)
for img in img_list:
img_dir = os.path.join(dir_old, img)
pic = cv2.imread(img_dir)
pic = scale(pic, fx, fy)
img_dir_new = os.path.join(dir_new, img)
cv2.imwrite(img_dir_new, pic)
if __name__ == "__main__":
main()