1. 程式人生 > >Python ,利用cv2批量縮放影象

Python ,利用cv2批量縮放影象

先將影象檔名稱讀取儲存為csv檔案,以下為讀取資料夾下字尾為.py的檔名稱的程式碼

#print(root) #當前目錄路徑  
#print(dirs) #當前路徑下所有子目錄  
#print(files) #當前路徑下所有非目錄子檔案 
import os
import csv

L=[]   
file_dir ='.'
i=0
with open('predict.csv','w') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['label'])
    for root, dirs, files
in os.walk(file_dir): for file in files: if os.path.splitext(file)[1]=='.py': L.append (os.path.join(file)) csvfile.write('\n'.join(L))

批量縮放資料夾下的影象

import os
import cv2
import numpy as np
import sys
import csv
import pandas as pd    
colunm=[]
with
open('E:/skin_2018/Training_GroundTruth/1.csv','rt',encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile) column = [row['image'] for row in reader] for i in column: img=cv2.imread('E:/2018/Task3_Training_Input/'+i+'.jpg') np.shape(img) img1 = cv2.resize(img,(256,256)) cv2.imwrite('E:/2018/2018_256/'
+i+'.jpg',img1) print (over)