File: set_iptc.py

package info (click to toggle)
libiptcdata 1.0.2%2Blibtool01-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,916 kB
  • ctags: 978
  • sloc: sh: 9,445; ansic: 4,092; xml: 2,798; makefile: 123; python: 62; sed: 16
file content (66 lines) | stat: -rw-r--r-- 1,949 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# sample Python program to set IPTC data in a JPEG file.

# This puts IPTC data in a format suitable for Flickr to get metadata
# about the photo; it is probably wrong for other uses.

import sys
from optparse import OptionParser

import iptcdata

def check_for_dataset(f, rs):
    for ds in f.datasets:
        if (ds.record, ds.tag) == rs:
            return ds
    return None

usage = "set_iptc.py [-c caption] [-t tag,tag,...] [-T title] filename"

parser = OptionParser(usage, version="0.1")

parser.add_option("-c", "--caption", dest="caption", action="store",
                  type="string", help="Caption data for photo")
parser.add_option("-t", "--tags", dest="tags", action="store",
                  type="string", help="Tags")
parser.add_option("-T", "--title", dest="title", action="store",
                  type="string", help="Title for photo")

(options, args) = parser.parse_args()

try:
    f = iptcdata.open(args[0])
except IndexError:
    print usage
    sys.exit(1)

# first do the "title" which flickr wants as a "headline"
if (options.title):
    # we want to replace this record set if it is there
    print "Setting \'Headline\' to %s" % (options.title)
    rs = iptcdata.find_record_by_name("Headline")
    ds = check_for_dataset(f, rs)
    if (ds == None):
        ds = f.add_dataset(rs)
    ds.value = options.title

# a "caption" gives the caption
if (options.caption):
    #also want just one caption
    print "Setting \'Caption\' to %s" % (options.caption)
    rs = iptcdata.find_record_by_name("Caption")
    ds = check_for_dataset(f, rs)
    if (ds == None):
        ds = f.add_dataset(rs)
    ds.value = options.caption

# now just add tags, as keywords
if (options.tags):
    tags = options.tags.split(",")
    rs = iptcdata.find_record_by_name("Keywords")
    for tag in tags:
        print "Setting \'Keyword\' to %s" % (tag)
        ds = f.add_dataset(rs)
        ds.value = tag
        
f.save()
f.close()