對於具有數字規律命名的圖像,可以按照下述的代碼進行統一縮放
# coding: utf-8
import cv2
import numpy as np
for i in range(1, 10):
# 讀取圖像
filePath = 'E:/' + str(i) + '.jpg'
img=cv2.imdecode(np.fromfile(filePath,dtype=np.uint8),-1)
# img = cv2.imread(filePath) # 如果用這個,則路徑不能有中文
print(np.shape(img))
img2 = cv2.resize(img, (0,0), fx=0.5, fy=0.5) # 按比例縮放
# img2 = cv2.resize(img, (1512,2016)) # 按指定像素大小縮放
# print(np.shape(img2))
cv2.imshow('2', img2) # 顯示圖像
# 保存圖像
cv2.imencode('.jpg', img2)[1].tofile('E:/' + str(i) + '_pressed.jpg')
cv2.waitKey(0) # 保證imshow有效