python opencv 批量缩放图片

tensorflow 训练图像识别模型,手机拍的照片太大,因此用opencv进行批量的图片缩放操作。

import os
import sys
import cv2 as cv
# 原图片存放文件夹
PATH_TO_TEST_IMAGES_DIR = r'D:\Particle_code_ocr\project\model3\static\image'
# 修改后存放文件夹
PATH_TO_TEST_IMAGES_DIR1 = r'D:\Particle_code_ocr\project\model3\static\image7\\'
# 读文件夹下所有图片名称
fileList = os.listdir(PATH_TO_TEST_IMAGES_DIR)
print(len(fileList))

print("开始修改尺寸!")
for image in fileList:
    img = cv.imread(PATH_TO_TEST_IMAGES_DIR + "\\" + image)
    H, W = img.shape[0], img.shape[1]
    if W < H:
        width = 300
        height = 400
    else:
        width = 400 
        height = 300
    
    newimage = cv.resize(img, (width,height), interpolation=cv.INTER_AREA)
    # 保存图片
    cv.imwrite(PATH_TO_TEST_IMAGES_DIR1 + image, newimage)

print("尺寸修改完毕!")

猜你喜欢

转载自blog.csdn.net/qq_40430818/article/details/115163774