python新建文件并复制,Python迭代df来创建文件夹和复制文件

我在Python中有一个数据帧。在import pandas as pd

inp = [{'image':'001.jpg', 'x':100, 'y':200,'w':100, 'h':200, 'brand':'test1'}, {'image':'001.jpg', 'x':100, 'y':200,'w':100, 'h':200, 'brand': 'test1'}, {'image':'001.jpg', 'x':100, 'y':200, 'w':100, 'h':200, 'brand': 'test2'}]

df = pd.DataFrame(inp)

print df

打印时的数据框如下所示:

^{pr2}$

第一列中列出的图像存储在特定目录中。我将其称为源目录。在src_path = '/var/www/html/projects/images/'

我必须遍历数据帧,从第一列中获取每个图像,根据坐标裁剪子集,并将裁剪后的图像保存在使用“folder”列创建的文件夹中。在

以下是目前为止的代码:import pandas as pd

import os

import shutil

import cv2

#read the data frame

df = pd.read_csv('annotations.csv')

src_path = '/var/www/html/projects/images/'

# create a master folder to store all cropped images in separate sub-directories created basis the name in brand column

if not os.path.exists(os.path.join(src_path,'imageProcessDir')):

os.mkdir(os.path.join(src_path,'imageProcessDir'))

dest_path = src_path+'imageProcessDir'

#create sub-directories for each brand

ub = df.brand.unique()

for u in ub:

os.mkdir(os.path.join(dest_path,u))

for index, rows in df.iterrows():

#read each image

image = cv2.imread(src_path+rows['image'])

#crop image

brand = image[rows['y']:rows['y']+rows['h'], rows['x']:rows['x']+rows['w']]

#save the cropped image in specific directories as listed in folder column

if not os.path.exists(os.path.join(dest_path, rows['brand'])):

shutil.move(brand, os.mkdir(os.path.join(dest_path, rows['brand'])));

我被困在最后一步,我需要在品牌子目录中保存裁剪后的图像。以下是所需的最终目录结构:dest_path

|

imageProcessDir

|__test1

|_001_1.jpg #brand = image[rows....will go here

|_001_2.jpg

|__test2

|_001_1.jpg

我可以重新命名最终输出。在

但是,我无法匹配df列下的目录名和imageProcessDir下的实际目录名。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值