heic转jpg python代码
做深度学习时,为了减少空间占用,会在拍照时使用heic格式,但是labelimg不能识别这个格式,所以还要转成jpg。
搜了一圈没有合适的,怒写这份代码
from PIL import Image
import pillow_heif
import piexif
from glob import glob
import sys
def heic_to_jpg(img_path,save_path):
name = (img_path.split('/')[-1]).split('.')[0]
# open the image file
heif_file = pillow_heif.read_heif(img_path)
#create the new image
image = Image.frombytes(heif_file.mode, heif_file.size, heif_file.data.tobytes(), "raw", heif_file.mode, heif_file.stride)
image.save(save_path+name+".jpg", "JPEG", quality=100) #默认转成jpg
files = glob("*.HEIC") #读取全部heic文件地址
save_path = "C:/Users/dumpling/Desktop/new/" #储存地址
for img in files:
print("Conversion process: ", img)
sys.stdout.flush()
heic_to_jpg(img,save_path)
print('--------------finish-------------')