Сохраните EXIF ​​после изменения размера и поворота изображения, затем сохраните в python - PullRequest
0 голосов
/ 22 ноября 2018

Я читал еще одну ветку на SO, касающуюся поворота изображения здесь: Миниатюра PIL вращает мое изображение?

, и я читал другую ветку на SO о сохранении EXIF ​​здесь: Сохранение exif-данных изображения с PIL при изменении размера (создание миниатюры)

К сожалению, после реализации приведенных выше советов кажется, что я могу иметь только: 1) сохраненное повернутое изображение без данных EXIFили 2) неповоротное изображение с данными EXIF, но кажется, что у меня не может быть обоих.

Я надеюсь, что смогу получить некоторую помощь, чтобы исправить то, что, по моему мнению, было действительно простой проблемой, но имеетпоследние несколько часов превращаюсь в крик на моем компьютере.

Вот соответствующие части моего кода:

from PIL import Image, ExifTags
import piexif
currImage = Image.open(inFileName)
exif_dict = piexif.load(currImage.info["exif"])

for orientation in ExifTags.TAGS.keys():
    if ExifTags.TAGS[orientation]=='Orientation':
        break
exif=dict(currImage._getexif().items())

if exif[orientation] == 3:
    currImage=currImage.rotate(180, expand=True)
elif exif[orientation] == 6:
    currImage=currImage.rotate(270, expand=True)
elif exif[orientation] == 8:
    currImage=currImage.rotate(90, expand=True)

currWidth, currHeight = currImage.size

# Here is where I can only do one or the other.  Don't know enough about how to get both
exif_bytes = piexif.dump(exif_dict)
#exif_bytes = piexif.dump(exif)

maxImageDimension = [1280, 640, 360, 160]
for imgDim in maxImageDimension:
    thumbRatio = imgDim / max(currWidth, currHeight)
    # note that because Python's round function is mathematically incorrect, I have to do the following workaround
    newWidth = int(Decimal(str(thumbRatio * currWidth)).quantize(Decimal('0.'), rounding=ROUND_UP))
    newHeight = int(Decimal(str(thumbRatio * currHeight)).quantize(Decimal('0.'), rounding=ROUND_UP))
    # copy currImage object
    newImage = currImage
    # note that I have to do resize method because thumbnail method has same rounding problem
    newImage = newImage.resize((newWidth, newHeight))
    # save the thumbnail
    if imgDim == 1280:
        outFileName = destinationDir + '\\' + file[:len(file)-4] + '.jpg'
    else:
        outFileName = destinationDir + '\\' + file[:len(file)-4] + '-' + str(newWidth) + 'x' + str(newHeight) + '.jpg'
    print('Writing: ' + outFileName)

    # Here is where I have to choose between exif or exif_bytes when saving but I can only get one or the other desired result
    newImage.save(outFileName, exif=exif_bytes)  

print('\n')

currImage.close()
newImage.close()

Заранее спасибо.

...