近期自网上下载的许多jpg格式图片,在PS和opencv中都无法打开,在查看图片格式信息时,才发现这些图片实际格式是heif/heic,而不理其扩展名所示的jpeg格式。在网上找了一些解决办法,包括一些AI给出的解决方案都无法解决这种以jpg文件扩展名的heif图片格式转换问题。综合各方方案,现将个人方案发布,供大家参考。
# -*- coding:utf8 -*-
import os
import glob
import shutil
import whatimage
import pillow_heif
from PIL import Image as pilImage
import argparse
gSrcPath=None
gDestPath=None
gShowInfo=False
def getPicFromDir(picPath):
'''获取指定目标下的所有picture文件列表'''
if not os.path.isdir(picPath):
return None
ext_names = ['.jpg', '.png', '.jpeg','*.webp','*.bmp','*.gif']
allImgs=[]
for ext in ext_names:
filenams=os.path.abspath(os.path.join(picPath,"./*%s" % ext))
allImgs.extend(glob.glob(filenams))
if len(allImgs)!=0 :
return allImgs
else:
return None
def convert_heic_jpg(src_