def _pyexiv2_old(self, fn): from pyexiv2 import Image exif = Image(fn) exif.readMetadata() keys = set(exif.exifKeys()) self._getitem = exif.__getitem__ self._contains = keys.__contains__
def get_image_comments(path): image = Image(path) image.readMetadata() comments = image.getComment() if not comments: return [] pieces = comments.split(COMMENT_DELIMINATOR) comments = [] print 'getting:',path for piece in pieces: data = {} sub_pieces = [x.strip() for x in piece.split(':') if x.strip()] # the first piece (if there is more than one) # is the label. if there is only one it's the body if len(sub_pieces) == 0: continue elif len(sub_pieces) == 1: body = data.get('body','') body += ('\n' if body else '') + sub_pieces[0] data['body'] = body else: # first is going to be the label w/ # possible sub labels if '[' in sub_pieces[0]: label = sub_pieces[0].split('[')[0] data['label'] = label sub_labels = sub_pieces[0][len(label):] sub_labels = sub_labels.replace('[') # the last one will be blank sub_labels = sub_labels.split(']')[:-1] data['sub_labels'] = sub_labels else: data['label'] = sub_pieces[0] print 'adding:',data comments.append(data) return comments
def set_image_comment(path,*args,**kwargs): """ takes any kwarg, to use as a labeled part of the comment. can also take single arg to use as body of comment """ if args and not kwargs: kwargs['body'] = '\n'.join(args) image = Image(path) image.readMetadata() # append is a possible kwarg append = True existing = image.getComment() or "" if append else "" comment_parts = [COMMENT_DELIMINATOR] if existing else [] for k,v in kwargs.iteritems(): if v is None: continue if k == 'append': append = v else: template = TEMPLATES.get(k,"%s:%s" % (k,v)) comment_parts.append(template % v) image.setComment(existing + "\n".join(comment_parts)) image.writeMetadata()
def upload(self, album, filename, title=None, tags=None, description=None, access_type=ACCESS.PUBLIC, disable_comments=False, xxx=False, hide_orig=False, storage_private=False, ): logging.debug('Uploading %r to album %s' % (filename, album)) tags = tags or u'' title = title or os.path.basename(filename) description = description or u'' try: from pyexiv2 import Image as ImageExif except: logging.warning('can\'t find python-pyexiv2 library, exif extraction will be disabled.') ImageExif = None if ImageExif: try: exif = ImageExif(filename) try: exif.readMetadata() try: tags = tags or u','.join(t for t in (smart_unicode(tag) \ for tag in exif['Iptc.Application2.Keywords']) if t) except KeyError: pass try: title = smart_unicode(title or exif['Iptc.Application2.ObjectName']) except KeyError: pass try: description = smart_unicode(description or \ exif['Iptc.Application2.Caption']) except KeyError: pass try: description = smart_unicode(description or \ exif['Exif.Image.ImageDescription']) except KeyError: pass except IOError: pass except IOError: pass def to_bool(value, yes = 'true', no = 'false'): if value in [True, 'yes', 1, 'true']: return yes return no data = dict( title=smart_str(title), tags=smart_str(tags), description=smart_str(description), access_type=ACCESS.tostring(access_type), disable_comments=to_bool(disable_comments), xxx=to_bool(xxx), hide_orig=to_bool(hide_orig), storage_private=to_bool(storage_private), pub_channel='Python API', app_platform=sys.platform, app_version=__version__, ) files = dict( image=open(smart_unicode(filename), 'rb'), ) response = self._post( album.links['photos'], data=data, files=files, extra_headers=dict( Slug = os.path.basename(filename) ), ) return Photo(self, response)