Python-Trick

33 阅读1分钟
  • PIL exif-orientation-handling

The reason for this is that this image has [Exif Orientation metadata] associated with it that will cause applications that respect that property to rotate it

# identify -verbose IMG_0004.JPG | grep Orientation
  Orientation: RightTop
    exif:Orientation: 6
from PIL import Image, ExifTags

def fix_landscape (img, thumb_size):
  THUMB_SIZE = (thumb_size,thumb_size)
  exif=dict((ExifTags.TAGS[k], v) for k, v in img._getexif().items() if k in ExifTags.TAGS)
  if exif['Orientation'] == 3 :
    img=img.rotate(180, expand=True)
  elif exif['Orientation'] == 6 :
    img=img.rotate(270, expand=True)
  elif exif['Orientation'] == 8 :
    img=img.rotate(90, expand=True)
  img
  • UTC to timestamp
from datetime import datetime, timezone
def s2u(utc_time_string):
   return datetime.strptime(utc_time_string, "%Y-%m-%d %H:%M:%S.%f %Z").replace(tzinfo=timezone.utc).timestamp()

int(s2u(''2017-03-02 23:25:10.07453 UTC''))
>>> 1488497110