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)
浙公网安备 33010602011771号