Пример #1
0
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
Пример #2
0
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()