当前位置:   article > 正文

python heic后缀图片文件转换成jpg格式_heic格式转化jpg代码

heic格式转化jpg代码

参考链接:

https://stackoverflow.com/questions/54395735/how-to-work-with-heic-image-file-types-in-python

https://github.com/david-poirier-csn/pyheif

https://pypi.org/project/pyheif/

直接上代码

heic_to_jpg.py

  1. import subprocess
  2. import os
  3. import io
  4. import whatimage
  5. import pyheif
  6. import traceback
  7. from PIL import Image
  8. def decodeImage(bytesIo):
  9. try:
  10. fmt = whatimage.identify_image(bytesIo)
  11. # print('fmt = ', fmt)
  12. if fmt in ['heic']:
  13. i = pyheif.read_heif(bytesIo)
  14. # print('i = ', i)
  15. # print('i.metadata = ', i.metadata)
  16. pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
  17. # print('pi = ', pi)
  18. pi.save('heeh.jpg', format="jpeg")
  19. except:
  20. traceback.print_exc()
  21. def read_image_file_rb(file_path):
  22. with open(file_path, 'rb') as f:
  23. file_data = f.read()
  24. return file_data
  25. if __name__ == "__main__":
  26. file_path = './test_image.HEIC'
  27. print('file_path = ', file_path)
  28. data = read_image_file_rb(file_path)
  29. # print('data = ', data)
  30. decodeImage(data)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/924213
推荐阅读
相关标签
  

闽ICP备14008679号