按照json文件的值复制图片

按照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)


参考1
参考2

代码封装

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)

参考1

参考2

参考3

相关推荐
big狼王5 小时前
sqlserver存储过程中入参使用JSON
数据库·sqlserver·json
西京刀客8 小时前
Go语言json.Marshal多态机制
算法·golang·json
孜然卷k9 小时前
前端处理后端对象类型时间格式通用方法封装,前端JS处理JSON 序列化后的格式 java.time 包中的日期时间类
前端·json
kooboo china.2 天前
什么是JSON ?从核心语法到编辑器
javascript·编辑器·json
黑眼圈的小熊猫2 天前
项目-- Json-Rpc框架
rpc·json·php
大千AI助手2 天前
django中如何解析content-type=application/json的请求
django·sqlite·json
胡斌附体3 天前
接口获取到数组进行json对象转换
json·router·json.parse
python_chai3 天前
Django核心知识点全景解析
python·json
子正4 天前
C环境下更简洁的cJSON辅助函数
json