当前位置:网站首页>JSON file to PNG image (batch conversion / image naming / migration / pixel value change) [tips]
JSON file to PNG image (batch conversion / image naming / migration / pixel value change) [tips]
2022-07-28 22:41:00 【It's too simple】
One . Code
Get ready : Contains many json File folder
Use : In the code json_folder Place to write json Folder address (‘/’ Keep the symbols ),png_folder Place to write png Folder address .
effect :json_folder The folder contains batch generated json Folder , There is less inside label.png picture ,png_folder Folders contain named / Change pixel ( It refers to a single channel grayscale image , The pixel value is only 255 and 0) Of png file .
import os
import shutil
import numpy as np
import cv2
from PIL import Image
#json File expansion ( transformation )
json_folder = '/'
json_name = os.listdir(json_folder)
os.system("activate labelme")
for i in range(len(json_name)):
if(os.path.splitext(json_name[i])[1] == ".json"):
json_path = json_folder + json_name[i]
os.system("labelme_json_to_dataset " + json_path)
#json To png Batch naming of / Transfer
png_folder= ''
os.makedirs(png_folder,exist_ok=True)
i = 1
for name in json_name:
if os.path.isdir(json_folder + name):
if i < 10: # 0-9
newname = "00" + str(i) + '.png'
elif i > 9 and i < 100: # 10-100
newname = "0" + str(i) + '.png'
elif i > 99 and i < 1000: # 100-1000
newname = str(i) + '.png'
i += 1
os.chdir(json_folder + name)
old_name = 'label.png'
os.rename(old_name, newname)
shutil.move(json_folder + name + "/" + newname, png_folder)
# Change pixel value
png_name = os.listdir(png_folder)
for name in png_name:
data_source = cv2.imread(png_folder +'/'+ name)
data = np.array(data_source)
img_path = png_folder + '/' + name
for i in range(data[:, :, 0].shape[0]):
for j in range(data[:, :, 0].shape[1]):
if data[:, :, 2][i][j] > 0 :
data[:, :, 2][i][j] = 255 #Red
data[:, :, 1][i][j] = 255 #Green
data[:, :, 0][i][j] = 255 #Blue
cv2.imwrite(img_path , data)
png = Image.open(img_path).convert('L').save(img_path)Two . explain
Preface
When I am doing paper reproduction and applying other data sets to this paper , The dataset lacks the truth map used in segmentation . So use labelme Do a good job in generating truth labels json file , It also needs to be converted into something that the code can use png Format truth map , Some of the functional problems encountered have been integrated ( I'm just a sewing monster of knowledge , Refer to the blog post at the end of the article ).
1.json File activation
labelme Generated json The file needs to activate the generation folder , Contains four documents .
2. transformation / name / transfer
In four documents png Format pictures are useful to us , So put each json In a folder png Extract to a folder , Complete the conversion .
Because it is a split test task , Use the pixel by pixel comparison of the original image and the truth map , The two figures need to be mapped one by one . Therefore, there are corresponding norms for image naming , Convenient for subsequent code sorted Function to sort , The code adopts the following naming format (eg:000,010,200).
3. Change the pixel value
After the above operation, a red truth map is obtained , It needs to be converted into a single channel gray image . Here, we use the method of modifying pixel values one by one and then converting them into gray-scale images , It's a little complicated in logic , But it also provides the idea of modifying pixel values in different channels .
4. Check
img_path Write the address of the picture you want to see , Zoom in to see the pixel value , It is found that the grayscale image of a single channel is , Be careful cv2.imread Inside cv2.IMREAD_UNCHANGED To place , Otherwise, the grayscale image will also be viewed as RGB chart .
import cv2
img_path = ''
img = cv2.imread(img_path,cv2.IMREAD_UNCHANGED)
cv2.imshow('photo',img)
key = cv2.waitKey(0)Reference resources
1. Batch conversion / transfer
2. Change pixel value
python- Modify the image size and RGB The pixel values of the three channels (opencv)_ Hula Hula hey blog -CSDN Blog _python Modify the image pixel size
Modify the pixel value of all gray-scale pictures under the folder _ Shensheng's brilliant blog -CSDN Blog
边栏推荐
- How about the actual use effect of common source oscilloscope
- Sword finger offer II 066. sum of words (medium prefix tree design string)
- STM32 - interrupt overview (interrupt priority)
- Which is the file transfer command in the basic services of the Internet
- 770. 单词替换
- Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
- Analysis notes on let (const) temporary dead zone in JS
- JS implementation generates a random key of specified length
- 32. Longest valid bracket (difficult stack string)
- The function of wechat applet to cut pictures
猜你喜欢

微信小程序剪切图片的功能

Att & CK preliminary understanding

Can the MySQL create statement be used to create a table structure and append new records

软考网络工程师

STM32 -- program startup process

Research cup element recognition multi label classification task based on ernie-3.0 cail2019 method

ATT&CK 威胁情报

842. 排列数字

LVS+KeepAlived高可用部署实战应用

79. Word search (medium string array matrix backtracking)
随机推荐
[reprint] the token token is used in the login scenario
ES6 concept
Redis related
JS implementation generates a random key of specified length
STM32 - DMA direct memory access controller (cubemx configures DMA)
MySQL built-in functions
ES6, deep copy, shallow copy
Excel-vba quick start (XIII. Common usage of date)
Container configuration starts redis cluster single machine 6 nodes 3 Master 3 slave
log4j漏洞 elk平台 处理方法 (logstah5.5.1)
imx6q gpio复用
Winserver operation and maintenance technology stack
Vscode ROS configuration GDB debugging error record
Binary source code, inverse code, complement code
Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)
When can I sign up for the 2022 class I constructor examination?
Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]
Summary of recent bugs
CMD common commands
Qt+FFmpeg环境搭建