当前位置:网站首页>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
边栏推荐
- JMeter installs third-party plug-ins plugins Manager
- STM32 - interrupt overview (interrupt priority)
- Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)
- Analysis notes on let (const) temporary dead zone in JS
- The blueprint of flask complements openpyxl
- 软考网络工程师
- Ngrok intranet penetration
- Ngx+sql environment offline installation log (RPM installation)
- [Ruiji takeout] day05 package management business development
- Target segmentation learning
猜你喜欢

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

JVM -- custom class loader

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

PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)
How do we do full link grayscale on the database?

STM32 - reset and clock control (cubemx for clock configuration)

Ultra detailed visual studio 2019 running littlevgl (lvgl) simulator

STM32 board level support package for keys
![Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]](/img/87/d943cc1e8169bb670414fbf7a322c5.jpg)
Paddlenlp is based on ernir3.0 text classification. Take the crime prediction task of cail2018-small dataset as an example [multiple tags]
![[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login](/img/53/c578e0d1428ea569fb412a20019924.png)
[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login
随机推荐
Command line agent: proxychains configuration
Summary of recent bugs
JS convert numbers to letters
纪念一下第一次写的线段树了喽(对应洛谷3372)
Ultra detailed visual studio 2019 running littlevgl (lvgl) simulator
When can I sign up for the 2022 class I constructor examination?
Static details of static members
What does GPRS network mean
Winserver operation and maintenance technology stack
MySQL installation and configuration (super detailed, simple and practical)
Redis related
SSH password free login
770. Word replacement
98. Verify binary search tree (medium binary search tree DFS)
Imx6q GPIO multiplexing
Sword finger offer II 055. Binary search tree iterator (medium binary search tree iterator)
PC side web page special effects (offset series, obtain the coordinates of the mouse in the box, pop-up drag effect, magnifying glass effect)
771. The longest consecutive character in a string
fatal error: io. h: No such file or directory
Integrating database Ecology: using eventbridge to build CDC applications