今天用手机拍照做了个小数据集,导出发现是heic格式图像,网上有在线的图像转换,不过需要一张一张转换,且限制次数,因此用python实现这个
import whatimage
import pyheif
from PIL import Image
import os
def decodeImage(bytesIo, index):
bytesIo = 'heic/' + bytesIo
with open(bytesIo, 'rb') as f:
data = f.read()
fmt = whatimage.identify_image(data)
if fmt in ['heic', 'avif']:
i = pyheif.read_heif(data)
pi = Image.frombytes(mode=i.mode, size=i.size, data=i.data)
pi.save("gzp/"+"new" + str(index) + ".jpg", format="jpeg")
# For my use I had my python file inside the same folder as the heic files
source = "heic/"
for index,file in enumerate(os.listdir(source)):
print(index)
print(file)
decodeImage(file, index)
```