'''
将单张图像的json转化成图片
'''
import json
import re
import cv2
import numpy as np
def toRgb(tmp):
opt = re.findall(r'(.{2})', tmp) # 将字符串两两分割
strs = "" # 用以存放最后结果
for i in range(0, len(opt)): # for循环,遍历分割后的字符串列表
strs += str(int(opt[i], 16)) + "," # 将结果拼接成12,12,12格式
aa = strs[0:-1]
aa.split(',')[0]
num = [int(aa.split(',')[0]), int(aa.split(',')[1]), int(aa.split(',')[2])]
return num
if __name__ == "__main__":
path = "./json_save_path/红河-烫金工艺-套准偏差_18_IPU3_534306_0.json"
with open(path, 'r') as load_f:
load_dict = json.load(load_f)
print(len(load_dict))
height = load_dict['Height']
width = load_dict['Width']
Lines = load_dict['Lines']
Polygon=load_dict['Polygons']
Bound = load_dict['FillRects']
img = np.zeros((width, height), dtype=np.uint8) # random.random()方法后面不能加数据类型
bgr_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
if (Bound is not None) and (len(Bound) != 0): #矩形标注
Bound = load_dict['FillRects']
Bounds1 = Bound[0]['Bounds']
a = [int(Bounds1.split(',')[0]), int(Bounds1.split(',')[1]), int(Bounds1.split(',')[2]),
int(Bounds1.split(',')[3])]
color = Bound[0]['Color']
tmp = color[1:]
value = toRgb(tmp)
value =(value[0],value[1],value[2])
print("hello")
triangle = np.array([[0, 0], [400, 0], [400, 400], [0, 400]])
# rect = np.arange(8).reshape((4, 2))
rect = np.array([[0 for col in range(2)] for row in range(4)])
rect[0][0] = int(a[0])
rect[0][1] = int(a[1])
rect[1][0] = int(a[2])
rect[1][1] = int(a[1])
rect[2][0] = int(a[2])
rect[2][1] = int(a[3])
rect[3][0] = int(a[0])
rect[3][1] = int(a[3])
cv2.fillConvexPoly(bgr_img,rect,color=value)
if len(Lines)==0 and len(Bound)==0:
# if (Lines is None) and (Bound is None):
bgr_img = bgr_img
if len(Lines) !=0: # 随机涂抹
for i in range(len(Lines)):
a = Lines[i]
color = a['Color']
tmp = color[1:]
value = toRgb(tmp)
points = a['Points']
stroke = a['Stroke']
arr = np.array([[0, 0]] * len(points))
# 将points 转化为array
for j in range(len(points)):
p = points[j]
pp1 = int(p.split(',')[0])
pp2 = int(p.split(',')[1])
arr[j, 0] = pp1
arr[j, 1] = pp2
print('Line finished!')
arr = arr.reshape(-1, 1, 2)
cv2.polylines(bgr_img, [arr], False, (value[2], value[1], value[0]), thickness=stroke)
cv2.imencode('.bmp', bgr_img)[1].tofile('图像_mask.bmp')
json中的内容:
{
"FillRects" : null,
"Height" : 200,
"Indexs" :
[
0
],
"Lines" :
[
{
"Color" : "#55AA00",
"Index" : 0,
"Points" :
[
"79,106",
"79,105",
"80,105",
"80,104",
"81,104",
"82,103",
"83,103",
"84,102",
"85,102",
"85,101",
"86,101",
"87,101",
"87,100",
"88,100",
"89,100",
"89,99",
"90,99",
"91,99",
"92,99",
"93,99",
"94,99",
"94,98",
"95,98",
"96,98",
"97,98",
"98,98",
"98,99",
"99,99",
"100,99",
"101,99",
"102,99",
"102,100",
"103,100",
"104,100",
"104,101",
"105,101",
"105,102",
"106,102",
"107,103",
"108,103",
"108,104",
"108,105",
"109,105",
"109,106",
"109,107",
"109,108",
"109,109",
"109,110",
"109,111",
"109,112",
"108,112",
"108,113",
"108,114",
"107,114",
"107,115",
"106,116",
"106,117",
"105,117",
"104,117",
"104,118",
"103,118",
"103,119",
"102,120"
],
"Stroke" : 10
}
],
"Operator" : "DL",
"Polygons" : null,
"Rubbers" : null,
"Shapes" :
[
1
],
"Width" : 200
}
生成的图像如下:
批量处理代码:
import json
import cv2
import numpy as np
import re
def toRgb(tmp):
opt = re.findall(r'(.{2})', tmp) # 将字符串两两分割
strs = "" # 用以存放最后结果
for i in range(0, len(opt)): # for循环,遍历分割后的字符串列表
strs += str(int(opt[i], 16)) + "," # 将结果拼接成12,12,12格式
aa=strs[0:-1]
aa.split(',')[0]
num=[int(aa.split(',')[0]),int(aa.split(',')[1]),int(aa.split(',')[2])]
return num
if __name__=="__main__":
file_list_path='D:/CF_new/json_to_img/json_name.txt'
path='D:/CF_new/json_to_img/saved_img/'
with open(file_list_path, "r+") as flist:
read_data = flist.read()
# 对单张图像进行处理
for eachline in read_data.split('\n'):
file_name = eachline.split('/')[-1].split('.')[0]
#读json文件
with open(eachline, 'r') as load_f:
load_dict = json.load(load_f)
print(len(load_dict))
height = load_dict['Height']
width = load_dict['Width']
Lines = load_dict['Lines']
Polygon = load_dict['Polygons']
Bound = load_dict['FillRects']
img = np.zeros((width, height), dtype=np.uint8) # random.random()方法后面不能加数据类型
bgr_img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
if (Bound is not None) and (len(Bound)!=0): # 矩形标注
Bound = load_dict['FillRects']
Bounds1 = Bound[0]['Bounds']
a = [int(Bounds1.split(',')[0]), int(Bounds1.split(',')[1]), int(Bounds1.split(',')[2]),
int(Bounds1.split(',')[3])]
color = Bound[0]['Color']
tmp = color[1:]
value = toRgb(tmp)
value = (value[2], value[1], value[0])
print("hello")
triangle = np.array([[0, 0], [400, 0], [400, 400], [0, 400]])
# rect = np.arange(8).reshape((4, 2))
rect = np.array([[0 for col in range(2)] for row in range(4)])
rect[0][0] = int(a[0])
rect[0][1] = int(a[1])
rect[1][0] = int(a[2])
rect[1][1] = int(a[1])
rect[2][0] = int(a[2])
rect[2][1] = int(a[3])
rect[3][0] = int(a[0])
rect[3][1] = int(a[3])
cv2.fillConvexPoly(bgr_img, rect, color=value)
if ((Lines is None) and (Bound is None) ):
bgr_img=bgr_img
if (Lines is not None): # 随机涂抹
for i in range(len(Lines)):
a = Lines[i]
color = a['Color']
tmp = color[1:]
value = toRgb(tmp)
points = a['Points']
stroke = a['Stroke']
arr = np.array([[0, 0]] * len(points))
# 将points 转化为array
for j in range(len(points)):
p = points[j]
pp1 = int(p.split(',')[0])
pp2 = int(p.split(',')[1])
arr[j, 0] = pp1
arr[j, 1] = pp2
print('Line finished!')
arr = arr.reshape(-1, 1, 2)
cv2.polylines(bgr_img, [arr], False, (value[2], value[1], value[0]), thickness=stroke)
# cv2.imwrite(path+file_name+'.bmp', bgr_img)
cv2.imencode('.bmp', bgr_img)[1].tofile(path+file_name+'.bmp')