前言
- 本文是个人使用Python处理文件的电子笔记,由于水平有限,难免出现错漏,敬请批评改正。
- 更多精彩内容,可点击进入我的个人主页查看
前提条件
- 熟悉Python
相关介绍
- Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。
- Python OS模块是负责程序与操作系统的交互,提供了访问操作系统底层的接口和非常丰富的方法用来处理文件和目录。
实验环境
- Python 3.x (面向对象的高级语言)
Python等比例缩放图片并修改对应的Labelme标注文件
Json文件
{
"version":"5.0.1",
"flags":{
},
"shapes":[
{
"label":"1",
"points":[
[
551.3333333333339,
17
],
[
1144.666666666667,
843.6666666666667
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
206.48387096774195,
1022.5806451612904
],
[
111,
0
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
2364.666666666667,
17
],
[
2704.666666666667,
1823.666666666667
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
698.272727272727,
3475.818181818182
],
[
1167.759862778731,
841.8233276157805
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
293.1515151515159,
4000.818181818181
],
[
707.363636363636,
848.5454545454545
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"4",
"points":[
[
109.70967741935465,
1025.8064516129032
],
[
193.58064516129025,
2103.2258064516127
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
1895.3243243243242,
13.513513513513514
],
[
2360.189189189189,
1208.1081081081081
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
1654.7837837837842,
710.8108108108108
],
[
2351.7692307692305,
3007.6923076923076
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
}
],
"imagePath":"1.jpg",
"imageData":null,
"imageHeight":4000,
"imageWidth":8320
}
代码实现
import os
import cv2
import json
def resize_img_equal_proportion(in_img_path,out_img_path,resized_pix):
'''
in_img_path:图片输入路径
out_img_path:图片结果保存路径
resized_pix: 缩放后分辨率
'''
ori_img = cv2.imread(in_img_path) # 读取图片
print(ori_img.shape)
height, width = ori_img.shape[:2] # 原始分辨率
# 等比例缩放到pix=400
scale = resized_pix / height
# 缩放后分辨率
resized_height = resized_pix
resized_width = int(width * scale)
img = cv2.resize(ori_img, (resized_width, resized_height))
print(img.shape)
cv2.imwrite(out_img_path, img)
return resized_height,resized_width,scale
def alter_json_equal_proportion(img_name,in_json_path,out_json_path,resized_height,resized_width,scale):
'''
in_json_path: json文件输入路径
out_json_path: json文件保存路径
resized_height: 缩放后的高
resized_width: 缩放后的宽
scale: 图片缩放比例
'''
file_in = open(in_json_path, "r", encoding='utf-8')
# json.load数据到变量json_data
json_data = json.load(file_in)
# 修改json中的内容
json_data["imageHeight"] = resized_height
json_data["imageWidth"] = resized_width
json_data["imagePath"] = img_name
for LabelBox in json_data['shapes']:
for point in LabelBox['points']:
point[0] = point[0]*scale
point[1] = point[1]*scale
file_in.close()
# 创建一个写文件
file_out = open(out_json_path, "w", encoding='utf-8')
# 将修改后的数据写入文件
file_out.write(json.dumps(json_data))
file_out.close()
if __name__ =="__main__":
in_dir_name = './test_resize_json/'
out_dir_name = in_dir_name + 'output/'
if not os.path.exists(out_dir_name):
os.mkdir(out_dir_name)
all_file_list = os.listdir(in_dir_name)
# print(all_file_list)
img_list = [i for i in all_file_list if i.endswith('.jpg') or i.endswith('.bmp') or i.endswith('.png') ]
# print(img_list)
json_list = [i for i in all_file_list if i.endswith('.json')]
# print(json_list)
for img_name in img_list:
# print(img_name)
in_img_path =in_dir_name + img_name # 获取文件路径
in_json_path = in_dir_name + img_name[:-4]+'.json'
out_img_path = out_dir_name + img_name
out_json_path = out_dir_name + img_name[:-4]+'.json'
resized_height,resized_width,scale = resize_img_equal_proportion(in_img_path,out_img_path,resized_pix=1024)
alter_json_equal_proportion(img_name,in_json_path,out_json_path,resized_height,resized_width,scale)
输出结果
{
"version":"5.0.1",
"flags":{
},
"shapes":[
{
"label":"1",
"points":[
[
141.14133333333348,
4.352
],
[
293.03466666666674,
215.9786666666667
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
52.85987096774194,
261.78064516129035
],
[
28.416,
0
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
605.3546666666667,
4.352
],
[
692.3946666666668,
466.85866666666675
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
178.75781818181812,
889.8094545454546
],
[
298.94652487135517,
215.5067718696398
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
75.04678787878808,
1024.2094545454543
],
[
181.08509090909084,
217.22763636363635
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"4",
"points":[
[
28.08567741935479,
262.60645161290324
],
[
49.556645161290305,
538.4258064516129
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
485.203027027027,
3.4594594594594597
],
[
604.2084324324325,
309.2756756756757
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
},
{
"label":"1",
"points":[
[
423.62464864864876,
181.96756756756758
],
[
602.052923076923,
769.9692307692308
]
],
"group_id":null,
"shape_type":"rectangle",
"flags":{
}
}
],
"imagePath":"1.jpg",
"imageData":null,
"imageHeight":1024,
"imageWidth":841
}
更多精彩内容,可点击进入Python日常小操作专栏或我的个人主页查看