How to use PIL to resize and apply rotation EXIF information to the file?

前端 未结 3 1020
轮回少年
轮回少年 2020-12-07 19:36

I am trying to use Python to resize picture. With my camera, files are all written is landscape way.

The exif information handle a tag to ask the image viewer to rot

3条回答
  •  醉梦人生
    2020-12-07 19:48

    Although PIL can read EXIF metadata, it doesn't have the ability to change it and write it back to an image file.

    A better choice is the pyexiv2 library. With this library it's quite simple flip the photo's orientation. Here's an example:

    import sys
    import pyexiv2
    
    image = pyexiv2.Image(sys.argv[1])
    image.readMetadata()
    
    image['Exif.Image.Orientation'] = 6
    image.writeMetadata()
    

    This sets the orientation to 6, corresponding to "Rotated 90 CW".

提交回复
热议问题