Преобразование HEIC в JPEG с метаданными

Я пытаюсь преобразовать файл heic в jpeg, импортируя также все метаданные (например, информацию о 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")

person Trics    schedule 28.11.2020    source источник
comment
Это может помочь stackoverflow .com/questions/53543549/   -  person jakub    schedule 28.11.2020
comment
Спасибо, я решил это через это:   -  person Trics    schedule 29.11.2020


Ответы (1)


Спасибо, я нашел решение, надеюсь поможет другим:

# 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)
person Trics    schedule 28.11.2020