4

我正在尝试在 jpeg 中转换 heic 文件,还导入所有元数据(如 gps 信息和其他东西),不幸的是,转换下面的代码是可以的,但没有元数据存储在创建的 jpeg 文件中。任何人都可以描述我需要在转换方法中添加什么?

heif_file = pyheif.read("/transito/126APPLE_IMG_6272.HEIC")
image = Image.frombytes(
    heif_file.mode,
    heif_file.size,
    heif_file.data,
    "raw",
    heif_file.mode,
    heif_file.stride,
)
image.save("/transito/126APPLE_IMG_6272.JPEG", "JPEG")
4

1 回答 1

5

谢谢,我找到了解决方案,希望可以帮助其他人:

# Open the file
heif_file = pyheif.read(file_path_heic)

# Creation of image 
image = Image.frombytes(
    heif_file.mode,
    heif_file.size,
    heif_file.data,
    "raw",
    heif_file.mode,
    heif_file.stride,
)
# Retrive the metadata
for metadata in heif_file.metadata or []:
    if metadata['type'] == 'Exif':
        exif_dict = piexif.load(metadata['data'])

# PIL rotates the image according to exif info, so it's necessary to remove the orientation tag otherwise the image will be rotated again (1° time from PIL, 2° from viewer).
exif_dict['0th'][274] = 0
exif_bytes = piexif.dump(exif_dict)
image.save(file_path_jpeg, "JPEG", exif=exif_bytes)
于 2020-11-28T21:26:54.490 回答