json转png

json文件转png标签

标签只有一类,最终结果为八位图

# json转png   开发时间:2023/5/13 21:19
import json
import os
import numpy as np
import cv2

def json_to_png(json_file_path, png_file_path):
    with open(json_file_path, 'r') as f:
        data = json.load(f)

    # 将json文件中的标注信息读取出来
    shapes = data['shapes']
    label_names = [shape['label'] for shape in shapes]
    # 获取每个标签对应的颜色值
    distinct_label_names = list(set(label_names))
    # 标签对应色彩字典
    label_name_to_color = {'1': (255, 255, 255)}
    # 创建空白的图片,并将每个像素点的值初始化为0
    img_height = data['imageHeight']
    img_width = data['imageWidth']
    img = np.zeros((img_height, img_width), dtype=np.uint8)
    # 为每个标注区域填充对应的颜色
    for shape in shapes:
        label_name = shape['label']
        color = label_name_to_color[label_name]
        points = shape['points']
        pts = np.array(points, np.int32)
        cv2.fillPoly(img, [pts], color=color)
    cv2.imwrite(png_file_path, img)


if __name__ == '__main__':

    json_file_path = 'LAB'
    json_fileList = os.listdir(json_file_path)
    png_file_path = '1'
    if not os.path.exists(png_file_path):
        os.makedirs(png_file_path)

    for json_file in json_fileList:
        a = json_file
        file_path_name = json_file_path + '/' + json_file
        # 截取文件名,用来命名图片
        index = json_file[:5] + '.png'
        png_file_path_i = png_file_path + '/' + index
        json_to_png(file_path_name, png_file_path_i)
        print(file_path_name, 'to', index)

 

posted @   yokon  阅读(184)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示