Example #1
0
def get_jpg_date(file_path):
    try:
        dt = MinimalExifReader(encode_path(file_path)).dateTimeOriginal()
        assert dt
        ret = "-".join(dt.split()[0].split(":"))
        if (len(ret) != 10 or 
            ret == '0000-00-00' or 
            int(ret.split('-')[0] < 2000)): return None
        return ret
    except:
        return None
Example #2
0
    def __init__(self, filename):
      self.filename = filename
      self.info = None
      self.exif = None

      try:
        self.info = IPTCInfo(self.filename, force=True)
      except:
        print "cannot initialize IPTCInfo!"
        pass

      try:
        self.exif = MinimalExifReader(self.filename)
      except:
        print "cannot initialize exif reader"
        pass

      # if we can't create either reader, give up
      if self.exif == None and self.info == None:
        print "cannot read file metadata"
        raise

      # at a minimum, we set a datestamp if one doesn't exist
      # set to current date & time
      exifDateStamp = None
      if self.exif != None:
        exifDateStamp = self.exif.dateTimeOriginal("%Y%m%d-%H%M%S")
        if exifDateStamp == "":
          exifDateStamp = None
      iptcDate = self.getData("dateCreated")
      iptcTime = self.getData("timeCreated")
  
      if exifDateStamp != None and \
        iptcDate == None and \
        iptcTime == None:
        datestamp = exifDateStamp.split('-')
        self.info.data[iptcMap['dateCreated']] = datestamp[0]
        self.info.data[iptcMap['timeCreated']] = datestamp[1]
      elif exifDateStamp == None and \
        iptcDate == None and \
        iptcTime == None:
        date, time = nowStamp()
        self.info.data[iptcMap['dateCreated']] = date
        self.info.data[iptcMap['timeCreated']] = time
Example #3
0
class Image:
    def __init__(self, filename):
      self.filename = filename
      self.info = None
      self.exif = None

      try:
        self.info = IPTCInfo(self.filename, force=True)
      except:
        print "cannot initialize IPTCInfo!"
        pass

      try:
        self.exif = MinimalExifReader(self.filename)
      except:
        print "cannot initialize exif reader"
        pass

      # if we can't create either reader, give up
      if self.exif == None and self.info == None:
        print "cannot read file metadata"
        raise

      # at a minimum, we set a datestamp if one doesn't exist
      # set to current date & time
      exifDateStamp = None
      if self.exif != None:
        exifDateStamp = self.exif.dateTimeOriginal("%Y%m%d-%H%M%S")
        if exifDateStamp == "":
          exifDateStamp = None
      iptcDate = self.getData("dateCreated")
      iptcTime = self.getData("timeCreated")
  
      if exifDateStamp != None and \
        iptcDate == None and \
        iptcTime == None:
        datestamp = exifDateStamp.split('-')
        self.info.data[iptcMap['dateCreated']] = datestamp[0]
        self.info.data[iptcMap['timeCreated']] = datestamp[1]
      elif exifDateStamp == None and \
        iptcDate == None and \
        iptcTime == None:
        date, time = nowStamp()
        self.info.data[iptcMap['dateCreated']] = date
        self.info.data[iptcMap['timeCreated']] = time

    def availableFields(self):
      return iptcMap.keys()

    def setData(self, field, data):
      if field in iptcMap:
        self.info.data[iptcMap[field]] = data

    def getData(self, field):
      if self.info != None:
        if field in iptcMap:
          return self.info.data[iptcMap[field]]

      return None
 
    def writeFile(self):
      self.info.save()

    def dump(self):
      for f in self.availableFields():
        print f + " : " + str(self.getData(f))