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("尺寸修改完毕!")