按照json文件的值复制图片

这篇具有很好参考价值的文章主要介绍了按照json文件的值复制图片。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

文件格式

按照json文件的值复制图片,缺陷检测,json
按照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)


按照json文件的值复制图片,缺陷检测,json
按照json文件的值复制图片,缺陷检测,json

参考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)

按照json文件的值复制图片,缺陷检测,json

参考1

参考2

参考3文章来源地址https://www.toymoban.com/news/detail-683281.html

到了这里,关于按照json文件的值复制图片的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • .net core 生成项目时.json配置文件没有复制到输出目录

    .net core 生成项目时.json配置文件没有复制到输出目录

    在程序运行时默认加载.exe文件同文件夹下的配置文件,而不是项目中的.json配置文件,所以需要把.json配置文件设置到自动生成目录,即下图所示:

    2023年04月13日
    浏览(10)
  • python之文件操作、对.txt文本文件的操作(读、写、修改、复制、合并)、对json文本文件的操作、json字符串与字典的相互转换。

    python之文件操作、对.txt文本文件的操作(读、写、修改、复制、合并)、对json文本文件的操作、json字符串与字典的相互转换。

    注意:本篇所讲的文件操作,都是对后缀为.txt、.json文件进行的操作。 1、json其实就是一个 文件的格式 ,像.txt一样,json也是一个 纯文本文件 。 与.txt不同的是, json常用来存放有键值对的数据,写法上像python中的字典一样, 但与字典不同的是 ,字典是一个数据类型,且

    2024年02月16日
    浏览(10)
  • 使用筛选后的coco数据集json文件,复制对应coco数据集图像

    有时候,我们需要对已经有的数据集进行数据划分,划分后的coco数据集需要寻找对应的图片,可以使用下面的程序脚本。 首先,导入三个必要的模块:json、os 和 shutil。json 模块用于读取 JSON 文件,os 模块用于处理文件路径,shutil 模块用于执行文件复制操作。 接下来,定义

    2024年02月01日
    浏览(6)
  • http发送和接收图片json文件

    1、先将图片转换为base64格式 2、将数据以json格式进行发送 其中 ImgInfo 类为: 上述代码中json数据有五个部分:image为图片数据,level是告警等级,rtsp为数据流地址,type是算法类型,label是算法标签等,所以数据发送为这五个内容。 HttpServer.cpp如下:   HttpServer.h如下: httpu

    2024年02月07日
    浏览(60)
  • Debezium系列之:多张表按照指定的字段分发数据到Kafka Topic,并保证多张表指定字段相同的值始终出现在相同分区

    根据productInfo和productorder两张表的productId分发数据,相同productId的数据始终出现在相同分区,并且希望两张表相同的订单数据也始终出现在相同分区。 例如关于productInfo表1206827245738502144这个订单的增删改数据始终出现在分区2,那productorder这个表1206827245738502144这个订单的增删

    2024年02月09日
    浏览(7)
  • 批量转换labelme标注后json文件为图片

    批量转换labelme标注后json文件为图片

    本文的代码可实现批量转换指定文件夹中的所有文件,抽取对应的图片至文件夹,标注图片转单通道以及实现分割区域在原图上的显示。 文章内容参考labelme批量制作数据集教程,写这个博客我主要是想自己加深一下印象,另外梳理一下代码,记录一下我自己遇到的问题,如

    2024年02月03日
    浏览(4)
  • opencv 六 缺陷检测实战2(PCB缺陷检测——小面积缺陷)

    opencv 六 缺陷检测实战2(PCB缺陷检测——小面积缺陷)

    对PCB进行缺陷检测,具体缺陷类型有开路(断路)、短路、缺口、毛刺。 上图为灰度图,黑色部分为电路板路线,其存在缺口、断路、毛刺、短路等缺陷。这些缺陷有的属于白色缺陷,有的属于黑色缺陷,但都属于小面积缺陷。故,可以使用opencv中的形态学算法,如:腐蚀、

    2024年02月02日
    浏览(9)
  • 【ChatGPT】利用ChatGPT将图片转换成JSON文件

    【ChatGPT】利用ChatGPT将图片转换成JSON文件

            我在创建自己的GPT时,通常会上传一些JSON文件作为知识库,我还制作了一些脚本工具,将PDF文件转换成JSON文件。但是在这个过程中产生一个问题,PDF文件中会有一些图表,JSON文件就不能存储和表达这些图表的内容了。那该怎么办呢?这里跟大家介绍一个方法,可

    2024年01月20日
    浏览(10)
  • 扩展element-ui el-upload组件,实现复制粘贴上传图片文件,带图片预览功能

    扩展element-ui el-upload组件,实现复制粘贴上传图片文件,带图片预览功能

    控件改造 在窗口的 el-form 控件参数中添加 @paste.native 事件,事件绑定方法名 handlePaste 也可以在其他控件中添加事件监听,看个人需求。 注意: 监听粘贴事件时,需要当前页面先获取焦点,否则无法正常监听, 可以在页面加载后调用 focus() 获取焦点 粘贴功能Js部分参考资料

    2023年04月08日
    浏览(50)
  • 如何将labelme生成的json文件转换成png图片(亲测有效)

    如何将labelme生成的json文件转换成png图片(亲测有效)

    单个转换(费时) 1.先进入到你存放json的文件夹的磁盘中,输入d: 2. 激活labelme环境 3. 输入labelme_json_to_dataset并进入到存放json的文件夹 4.完成转换 批量转换(等我更新) 更新来啦!!!(步骤) 1、修改json_to_dataset.py代码 这里它的地址为D:Anaconda3envslabelmeLibsite-packageslabe

    2024年02月13日
    浏览(6)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包