按照json文件的值复制图片

按照json文件的值复制图片

文件格式

在这里插入图片描述
在这里插入图片描述

0是不挑选,1是挑选

处理当前JSON

# 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

代码封装

# 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




增加批处理

# 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值