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

相关推荐
bigcarp1 小时前
requests 中data=xxx、json=xxx、params=xxx 分别什么时候用
json
Spider Cat 蜘蛛猫7 小时前
关于vscode中settings.json中的设置
vscode·json·eslint
Json_1817901448021 小时前
商品详情接口使用方法和对接流程如下
大数据·json
ZhongruiRao2 天前
Springboot+PostgreSQL+MybatisPlus存储JSON或List、数组(Array)数据
spring boot·postgresql·json
华农第一蒟蒻2 天前
Java中JWT(JSON Web Token)的运用
java·前端·spring boot·json·token
胡耀超2 天前
知识图谱入门——8: KG开发常见数据格式:OWL、RDF、XML、GraphML、JSON、CSV。
xml·json·知识图谱·csv·owl·graphml·gml
x-cmd2 天前
[241005] 14 款最佳免费开源图像处理库 | PostgreSQL 17 正式发布
数据库·图像处理·sql·安全·postgresql·开源·json
先知demons2 天前
js将对象的键和值分别归纳进对象,并将多层对象转化成数据的方法
javascript·vue.js·json
Midsummer啦啦啦3 天前
Python字符串转JSON格式指南
开发语言·python·json
前端 贾公子3 天前
Express内置的中间件(express.json和express.urlencoded)格式的请求体数据
中间件·json·express