代码涉及到:遍历目标路径,选取csv后缀的文件,遍历csv每一行,读取坐标,用cv操作图片
# !/usr/bin/python # -*- coding: UTF-8 -*- import pandas as pd import os import time import scipy.misc from shutil import copyfile from PIL import Image import shutil g_img = None def saveToMysql(row): sides = [50,100,200,300,400,500,600] img = g_img limit_x = img.shape[1] limit_y = img.shape[0] for side in sides: x1 = row.X - side x2 = row.X + side y1 = row.Y - side y2 = row.Y + side x1 = min(limit_x, max(0, x1)) x2 = max(x1+1, min(limit_x, x2)) y1 = min(limit_y, max(0, y1)) y2 = max(0, min(limit_y, y2)) t1 = int(time.time()*1000) cropped = img[y1:y2,x1:x2,:] t2 = int(time.time()*1000) #new_crop_parh = row.img + '_' + str(row.Type) + '_' + str(row.X) + '_' + str(row.Y) + '.png' new_crop_parh = 'img_crop/' + str(row.Type) + '/' + row.crop + '_' + str(row.X) + '_' + str(row.Y) + '_' + str(side) + '.png' scipy.misc.imsave(new_crop_parh , cropped) t3 = int(time.time()*1000) # print("剩余时间:",(t3-t2)*18441/1000/3600) if __name__ == '__main__': for i in range(1,17): path = './img_crop/' + str(i) #if os.path.exists(path): # shutil.rmtree(path+'/') #os.mkdir(path) for line in open("xae"): csvpath = line.strip(' ') if not csvpath or not os.path.exists(csvpath): print('not found: ' + csvpath) arr = csvpath.split('/') image = arr[4].replace(".csv", ".JPG") medicalId=arr[3] batchId=arr[1] img_path = 'img/' + batchId + '/' + medicalId + '/Images/' + image #print(img_path) img_path_crop_path = batchId + '_' + medicalId + '_' + image # print(csvpath) df = pd.read_csv(csvpath) #print(df) df['img'] = img_path df['crop'] = img_path_crop_path g_img = scipy.misc.imread(img_path) df.apply(saveToMysql, axis=1)