write_json.py 867 B

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