按照json文件的值复制图片
文件格式
0是不挑选,1是挑选
处理当前JSON
python
# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json
def read_json(file_name):
with open(file_name, 'rb') as f:
data = json.load(f)
return data
def zhuanhuan(i):
if i == 0:
return "0_0.bmp"
elif i == 1:
return "0_1.bmp"
elif i == 2:
return "0_2.bmp"
elif i == 3:
return "0_3.bmp"
elif i == 4:
return "0_4.bmp"
elif i == 5:
return "1_0.bmp"
elif i == 6:
return "1_1.bmp"
elif i == 7:
return "1_2.bmp"
elif i == 8:
return "1_3.bmp"
elif i == 9:
return "1_4.bmp"
elif i == 10:
return "2_0.bmp"
elif i == 11:
return "2_1.bmp"
elif i == 12:
return "2_2.bmp"
elif i == 13:
return "2_3.bmp"
elif i == 14:
return "2_4.bmp"
elif i == 15:
return "3_0.bmp"
elif i == 16:
return "3_1.bmp"
elif i == 17:
return "3_2.bmp"
elif i == 18:
return "3_3.bmp"
elif i == 19:
return "3_4.bmp"
elif i == 20:
return "4_0.bmp"
elif i == 21:
return "4_1.bmp"
elif i == 22:
return "4_2.bmp"
elif i == 23:
return "4_3.bmp"
elif i == 24:
return "4_4.bmp"
if __name__ == "__main__":
path = "D:\\code\\select\\1\\"
file_dict = {}
select_list = []
file_dict = read_json("./1/kuaisu.json")
# print(file_dict)
a = len(file_dict)
# print(a)
print(file_dict["1_1.bmp"])
print(zhuanhuan(1))
print("开始转换")
for x in range(len(file_dict)):
name = zhuanhuan(x)
#print(file_dict[name])
select_list.append(file_dict[name])
#print(select_list)
#shutil.copy("D:\\code\\select\\1\\0_0.bmp", "D:\\code\\select\\0_0.bmp")
#读取数组和拷贝图片
for x in range(len(select_list)):
if select_list[x]==1:
old_path = path+zhuanhuan(x)
new_path1 = "D:\\code\\select\\final\\"
new_path2 = new_path1 + zhuanhuan(x)
if os.path.exists(new_path1):
shutil.copy(old_path,new_path2)
##print('')
else:
os.mkdir(new_path1) # 创建pic文件夹,用于保存图片
shutil.copy(old_path,new_path2)
代码封装
python
# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json
def read_json(file_name):
with open(file_name, 'rb') as f:
data = json.load(f)
return data
def zhuanhuan(i):
if i == 0:
return "0_0.bmp"
elif i == 1:
return "0_1.bmp"
elif i == 2:
return "0_2.bmp"
elif i == 3:
return "0_3.bmp"
elif i == 4:
return "0_4.bmp"
elif i == 5:
return "1_0.bmp"
elif i == 6:
return "1_1.bmp"
elif i == 7:
return "1_2.bmp"
elif i == 8:
return "1_3.bmp"
elif i == 9:
return "1_4.bmp"
elif i == 10:
return "2_0.bmp"
elif i == 11:
return "2_1.bmp"
elif i == 12:
return "2_2.bmp"
elif i == 13:
return "2_3.bmp"
elif i == 14:
return "2_4.bmp"
elif i == 15:
return "3_0.bmp"
elif i == 16:
return "3_1.bmp"
elif i == 17:
return "3_2.bmp"
elif i == 18:
return "3_3.bmp"
elif i == 19:
return "3_4.bmp"
elif i == 20:
return "4_0.bmp"
elif i == 21:
return "4_1.bmp"
elif i == 22:
return "4_2.bmp"
elif i == 23:
return "4_3.bmp"
elif i == 24:
return "4_4.bmp"
def my_copy(json_path,old_path,new_path):
path = old_path
file_dict = {}
select_list = []
file_dict = read_json(json_path)
a = len(file_dict)
print(file_dict["1_1.bmp"])
print(zhuanhuan(1))
print("开始转换")
for x in range(len(file_dict)):
name = zhuanhuan(x)
select_list.append(file_dict[name])
for x in range(len(select_list)):
if select_list[x]==1:
old_path = path+zhuanhuan(x)
new_path1 = new_path
new_path2 = new_path1 + zhuanhuan(x)
if os.path.exists(new_path1):
shutil.copy(old_path,new_path2)
else:
os.mkdir(new_path1) # 创建pic文件夹,用于保存图片
shutil.copy(old_path,new_path2)
if __name__ == "__main__":
#my_copy("./1/kuaisu.json","D:\\code\\select\\1\\","D:\\code\\select\\final\\")
my_copy
增加批处理
python
# coding: utf-8
from PIL import Image, ImageDraw, ImageFont
import os
import shutil
import cv2 as cv
import numpy as np
import json
def read_json(file_name):
with open(file_name, 'rb') as f:
data = json.load(f)
return data
def zhuanhuan(i):
if i == 0:
return "0_0.bmp"
elif i == 1:
return "0_1.bmp"
elif i == 2:
return "0_2.bmp"
elif i == 3:
return "0_3.bmp"
elif i == 4:
return "0_4.bmp"
elif i == 5:
return "1_0.bmp"
elif i == 6:
return "1_1.bmp"
elif i == 7:
return "1_2.bmp"
elif i == 8:
return "1_3.bmp"
elif i == 9:
return "1_4.bmp"
elif i == 10:
return "2_0.bmp"
elif i == 11:
return "2_1.bmp"
elif i == 12:
return "2_2.bmp"
elif i == 13:
return "2_3.bmp"
elif i == 14:
return "2_4.bmp"
elif i == 15:
return "3_0.bmp"
elif i == 16:
return "3_1.bmp"
elif i == 17:
return "3_2.bmp"
elif i == 18:
return "3_3.bmp"
elif i == 19:
return "3_4.bmp"
elif i == 20:
return "4_0.bmp"
elif i == 21:
return "4_1.bmp"
elif i == 22:
return "4_2.bmp"
elif i == 23:
return "4_3.bmp"
elif i == 24:
return "4_4.bmp"
def my_copy(json_path,old_path,new_path):
path = old_path
file_dict = {}
select_list = []
file_dict = read_json(json_path)
a = len(file_dict)
#print(file_dict["1_1.bmp"])
#print(zhuanhuan(1))
#print("开始转换")
for x in range(len(file_dict)):
name = zhuanhuan(x)
select_list.append(file_dict[name])
for x in range(len(select_list)):
if select_list[x]==1:
old_path = path+zhuanhuan(x)
new_path1 = new_path
new_path2 = new_path1 + zhuanhuan(x)
if os.path.exists(new_path1):
shutil.copy(old_path,new_path2)
else:
os.makedirs(new_path1) # 创建pic文件夹,用于保存图片
shutil.copy(old_path,new_path2)
print(old_path)
print(new_path2)
print("-----------")
if __name__ == "__main__":
#my_copy("./1/kuaisu.json","D:\\code\\select\\1\\","D:\\code\\select\\final\\")
image_root_path = "D:\标注完成"
suffix = ".json"
for root, dirs, files in os.walk(image_root_path):
# 使用os模块获取文件夹中所有文件的路径
all_files = os.listdir(root)
# 筛选以指定后缀名结尾的文件
filtered_files = [file for file in all_files if file.endswith(suffix)]
if(len(filtered_files)):
#print(root) #当前工作文件夹
json_path = root+"\\kuaisu.json"
old_path = root+"\\"
new_path = root.replace("标注完成","汇总")+"\\"
my_copy(json_path,old_path,new_path)
![](https://file.jishuzhan.net/article/1696741563977175042/d9f02fab149f49a8868e7ea43bf4de27.png)