引言
Gnt格式解析
import struct
from pathlib import Path
from PIL import Image
from multiprocessing import Pool
def process_gnt_file(gnt_paths):
label_list = []
for gnt_path in gnt_paths:
count = 0
print(f'gnt路径--->{gnt_path}')
with open(str(gnt_path), 'rb') as f:
while f.read(1) != "":
f.seek(-1, 1)
count += 1
try:
# 按类型提取gnt格式文件中的数据
length_bytes = struct.unpack('<I', f.read(4))[0]
tag_code = f.read(2)
width = struct.unpack('<H', f.read(2))[0]
height = struct.unpack('<H', f.read(2))[0]
im = Image.new('RGB', (width, height))
img_array = im.load() # 返回像素值
for x in range(height):
for y in range(width):
# 读取像素值
pixel = struct.unpack('<B', f.read(1))[0]
# 赋值
img_array[y, x] = (pixel, pixel, pixel)
filename = str(count) + '.png'
# 转换为中文的格式
tag_code = tag_code.decode('gbk').strip('\x00')
save_path = f'{save_dir}/zf_images_train/{gnt_path.stem}'
if not Path(save_path).exists(