将单张图像的json转化成图片
'''
将单张图像的json转化成图片
'''
import json
import re
import cv2
import numpy as npdef 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 numif __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_imgif 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 转化为arrayfor j in range(len(points)):p = points[j]pp1 = int(p.split(',')[0])pp2 = int(p.split(',')[1])arr[j, 0] = pp1arr[j, 1] = pp2print('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 redef 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 numif __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_imgif (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 转化为arrayfor j in range(len(points)):p = points[j]pp1 = int(p.split(',')[0])pp2 = int(p.split(',')[1])arr[j, 0] = pp1arr[j, 1] = pp2print('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')
将单张图像的json转化成图片相关推荐
- lableme json转化为图片常用的脚本
个人记录使用,针对的是16位图像 1. 数据集对比度拓展 import cv2 import os import numpy as np from PIL import Image # 图像混合# f ...
- iOS 文字转化成图片
//文字转化成图片 -(UIImage *)imageFromText:(NSArray*)arrContent withFont:(CGFloat)fontSize withTextColor:(U ...
- python 脚本将视频转化成图片 | python scripts to convert video to pictures
python 脚本将视频转化成图片 | python scripts to convert video to pictures python video_to_images.py 运行时,修改输入.输 ...
- python html转图片失真_html dom 转化成图片踩坑记(canvas toDataURL)
需求 在开发过程中遇到这么一个需求,h5页面需要将一个html dom转化成图片,便于用户保存. 面向百度搜索第三方得 html2canvas 和 dom-to-image 两者在写这篇笔记之前在gi ...
- php网址图片怎么转based4,Ionic4 Base64 转化成图片插件-Base64 转化成图片Base64 To Gallery - Ionic Native...
This plugin allows you to save base64 data as a png image into the device Ionic Base64 转化成图片Base64 T ...
- base64字符串转化成图片
/*** base64字符串转化成图片** @param base64* @return*/ public static File base64ToFile(String base64) throws ...
- base64编码转换android,Android将base64编码转化成图片
类似base64流的图片解析并展示: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAUDBAQEAwUEBAQFBQUGBwwIBw ...
- 手写画板实现并转化成图片
手写画板实现并转化成图片 <!DOCTYPE html> <html> <head> <title>画板实验</title><meta ...
- uni-app页面部分模块转化成图片并保存(适用app和h5)以及涉及轮播滚动时,区分轮播内容生成图片方法
上一篇uni-app的截屏是截取整个页面的内容,这一篇描述页面中一部分模块转化成图片并保存的方法. 效果描述: (页面:上面是一个卡片加二维码/条形码,下面是一个轮播,可以切换不同的卡片,轮播如果把i ...
最新文章
- 深度!移动机器人(AGV)产业链全分析
- Thrift RPC 系列教程(5)—— 接口设计篇:struct enum设计
- 学习Linux计划书
- Linux下安装Java(JDK8)
- 动画体系知识梳理(1) 转场动画 ContentTransition 理论篇
- 为什么魂斗罗只有 128 KB却可以实现那么长的剧情?
- VC++动态链接库编程之MFC DLL
- 如何让listView加入的HeaderView不可点击【转】
- 程序员的前20个搜索和排序算法面试问题
- 2012年I / O之后
- (转) Hibernate持久化类与主键生成策略
- (未解决)SpringMVC学习——为什么网址不是locahost而是desktop-nottqjs(如图)
- Mysql之to_base64编码from_base64解密和AES_ENCRYPT加密AES_DECRYPT解密
- 【机器视觉】Halcon 18安装教程
- PostgreSQL使用PgAdmin导入数据
- 二十八宿距星位置计算
- 男子订民宿被毁约5个家庭漂泊街头 房东:住满了,没办法
- 对拉格朗日乘数法的直观认识
- 猜数字游戏java课程设计报告
- 信息检索中 (IR) 的评价指标: P@n, MAP, MRR, DCG, NDCG