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

相关推荐
Mr.小怪7 小时前
【开源】开发了一个在终端里运行的轻量级Excel:可以查看、简单编辑、转JSON
json
CHQIUU17 小时前
在 C# .NET 中驾驭 JSON:使用 Newtonsoft.Json 进行解析与 POST 请求实战
c#·json·.net
i_am_a_div_日积月累_20 小时前
git检查提交分支和package.json的version版本是否一致
git·json
Java Fans1 天前
WPF使用SQLite与JSON文本文件结合存储体侧平衡数据的设计与实现
sqlite·json·wpf
还不如ctrC+V1 天前
VScode在 Markdown 编辑器中预览
node.js·json
一个天蝎座 白勺 程序猿2 天前
Python爬虫(8)Python数据存储实战:JSON文件读写与复杂结构化数据处理指南
爬虫·python·json
一路向北he2 天前
pcm数据不支持存储在json里面,需要先转base64
json·pcm
沉迷...4 天前
详解.vscode 下的json .vscode文件夹下各个文件的作用
ide·vscode·json
聪明的墨菲特i4 天前
SQL进阶知识:九、高级数据类型
xml·数据库·sql·mysql·json·空间数据类型
AAA顶置摸鱼4 天前
使用 Pandas 进行多格式数据整合:从 Excel、JSON 到 HTML 的处理实战
json·excel·pandas