main.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/09/27 14:57:31
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : enter point
  8. mastergo 保存的sketch文件,解压后,修改json文件,将所有的webp图片转换为png,然后重新打包为sketch文件
  9. '''
  10. import os,sys,re,json
  11. def fix_sketch(file_name:str, file_path:str):
  12. # unzip the sketch
  13. tmp_folder=file_path.split('.')[0]
  14. if not os.path.exists(tmp_folder):
  15. os.mkdir(tmp_folder)
  16. os.system('"C:\Program Files\Bandizip\bc.exe" c -target:auto -o:{}'.format(tmp_folder))
  17. os.chdir(tmp_folder)
  18. # fix the sketch
  19. for page in os.listdir('pages'):
  20. if re.match(r'.*\.json$', page):
  21. page=json.load(open('pages/{}'.format(page),'r'))
  22. # all image webp to png
  23. for layer in page['layers']:
  24. if layer['type']=='Image':
  25. if 'image' in layer:
  26. if 'webp' in layer['image']:
  27. layer['image']['webp']=layer['image']['webp'].replace('.webp','.png')
  28. for image in os.listdir("images"):
  29. if re.match(r'.*\.webp$', image):
  30. os.system('dwebp imagmes/{} -o imagmes/{}.png'.format(image,image.split('.')[0]))
  31. os.remove('imagmes/{}'.format(image))
  32. # "C:\Program Files\Bandizip\bc.exe" c aa -dir mastergo_sketch_fix
  33. os.system('zip -r {}.sketch .'.format(file_path.split('.')[0]))
  34. # rm -rf tmp dir
  35. def main():
  36. current_path=os.getcwd()
  37. for file in os.listdir():
  38. tmp_path=os.path.join(current_path, file)
  39. if os.path.isfile(tmp_path):
  40. if re.match(r'.*\.sketch$',file):
  41. fix_sketch(file,tmp_path)
  42. if __name__=='__main__':
  43. main()