def post_thumb(local_file, s3_file, key): """ Notify photostreamer-server that a new thumbnail photo has been posted to S3. """ l.debug("Notifying photostreamer-server of thumbnail photo %s", key) tags = exif.parse(local_file) filesize = os.path.getsize(local_file) payload = { "sender" : sender, "filesize": filesize, "fileid": key, "thumbnail": s3_file.generate_url(expires_in=0, query_auth=False), "exif": tags } success = post('/photo/thumb', payload)
def get(self): self.response.headers['Content-Type'] = 'application/json' # Get the querystring params image_url = self.request.get("url", None) callback = self.request.get("callback", None) logging.debug("Fetching %s", image_url) # Download the image from the passed URL param in QS response = fetch(image_url) if self.isValidFileExtension(image_url) == False: logging.error("Invalid image extension: %s", image_url) self.response.out.write("{ error: 'Image does not look valid based on its filename: " + image_url + "' }") return # If there was a problem fetching the URL grab the default logo if response.status_code != 200: logging.error("Error fetching image %s", image_url) self.response.out.write("{ error: 'Error fetching image: " + image_url + "'}") return image_content = response.content image_info = self.getImageInfo(image_content) image_content_type = image_info[0] image_width = image_info[1] image_height = image_info[2] image_byte_size = image_info[3] image_exif_tags = exif.parse( StringIO.StringIO(image_content), 255, mode=exif.ASCII ) template_values = { 'url': image_url, 'mime_type': image_content_type, 'width': image_width, 'height': image_height, 'byte_size': image_byte_size, 'callback': callback, 'exif': image_exif_tags } path = os.path.join(os.path.dirname(__file__), 'response.json') self.response.out.write(template.render(path, template_values))
def getExifCreationDate(path): """Gets the earliest date from the file's EXIF header, returns time tuple""" timeStamp = None try: import exif pf = exif.parse(path) originalTime = pf.get('DateTimeOriginal') if (originalTime): timeStamp = datetime.strptime(originalTime, '%Y:%m:%d %H:%M:%S') except: pass #sometimes exif lib failes to retrieve data if (not timeStamp): response = os.popen(__path_to_exif + ' -x "%s"' % path, 'r') lines = response.read() matches = re.findall('<Date_and_Time.+?>(.*?)</Date_and_Time.+?>', lines) if (len(matches)): timeStamp = min(*[datetime.strptime(x, '%Y:%m:%d %H:%M:%S') for x in matches]) return timeStamp
def getExifCreationDate(path): """Gets the earliest date from the file's EXIF header, returns time tuple""" timeStamp = None try: import exif pf = exif.parse(path) originalTime = pf.get('DateTimeOriginal') if (originalTime): timeStamp = time.strptime(originalTime, '%Y:%m:%d %H:%M:%S') except: pass #sometimes exif lib failes to retrieve data if (not timeStamp): response = os.popen(__path_to_exif + ' -x "%s"' % path, 'r') lines = response.read() matches = re.findall('<Date_and_Time.+?>(.*?)</Date_and_Time.+?>', lines) if (len(matches)): timeStamp = min(*[time.strptime(x, '%Y:%m:%d %H:%M:%S') for x in matches]) return timeStamp
def exif_map(files): for i, f in enumerate(files): print i, '/', len(files), f[f.index('.')+1:] try: info = exif.parse(f) except: print 'exception' if 'Model' in info and info['Model'] != 'Canon EOS REBEL T3i': continue for k in set(info.keys()) & keys: if k == 'DateTime': # cluster by day value = info[k][0:10] else: value = info[k] if value not in attr[k]: attr[k][value] = 1 else: attr[k][value] += 1
def exif_getdate(filename): """ Rename <old_filename> with the using the date/time created or modified for the new file name""" created_time = os.path.getctime(filename) modify_time = os.path.getmtime(filename) # f = open(filename, 'rb') try: tags = exif.parse(filename) except UnboundLocalError: print "No EXIF data available for ", file tags = {} exif_time = 0 try: tags['DateTimeOriginal'] exif_time = str(tags['DateTimeOriginal']) exif_time = int( time.mktime(time.strptime(exif_time, "%Y:%m:%d %H:%M:%S"))) except (KeyError, ValueError): print 'No EXIF DateTimeOriginal for ', file exif_time = 0 if created_time < modify_time: local_time = time.localtime(created_time) else: local_time = time.localtime(modify_time) if exif_time: if exif_time < local_time: local_time = time.localtime(exif_time) date_time_name = time.strftime(DATE_FORMAT, local_time) #print 'Created Time = ', created_time #print 'Modified Time = ', modify_time #print 'EXIF Time = ', exif_time #print 'Time Used = ', local_time return date_time_name
def others(): import exif photo_path = "america.jpg" data = exif.parse(photo_path)