import json from base64 import b64encode def make_json(img_fold, json_file_path, shapes_list, image_path, image_height, image_width): flags = {} # 读取二进制图片,获得原始字节码 with open(str(img_fold) + '\\' + image_path, 'rb') as jpg_file: byte_content = jpg_file.read() jpg_file.close() # 把原始字节码编码成base64字节码 base64_bytes = b64encode(byte_content) # 把base64字节码解码成utf-8格式的字符串 base64_string = base64_bytes.decode('utf-8') data = {'version': '5.0.1', 'flags': flags, 'shapes': shapes_list, 'imagePath': image_path, 'imageData': base64_string, 'imageHeight': image_height, 'imageWidth': image_width} with open(str(img_fold) + '\\' + json_file_path, 'w') as fp: json.dump(data, fp, sort_keys=False, indent=2) fp.close() return