misc changes from before -- date,etc
[nixpics.git] / bin / libnip.py
blob8ef3d96762d95a480bba1e5a04c63fb2c7d9a444
1 #!/usr/bin/env python
3 # nixpics -- UNIX-style photo-managing utilities
5 # Copyright (C) 7, Evan Battaglia <gtoevan@gmx.net>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 import pyexiv2
23 import datetime
24 import os
26 NIP_VERSION = "0.0.1"
28 NAME_TAG = 'Iptc.Application2.Headline'
29 TAGS_TAG = 'Iptc.Application2.Keywords'
30 DESC_TAG = 'Iptc.Application2.Caption'
31 DATE_TAG = 'Exif.Image.DateTime'
32 WIDTH_TAG = 'Exif.Photo.PixelXDimension'
33 HEIGHT_TAG = 'Exif.Photo.PixelYDimension'
35 # general purpose enum
36 TYPE_NAME=0
37 TYPE_DESC=1
38 TYPE_TAGS=2
40 class Image(pyexiv2.Image):
42 def __init__(self,f):
43 pyexiv2.Image.__init__(self,f)
44 self.filename = f
45 pyexiv2.Image.readMetadata(self)
47 def get_name(self):
48 if NAME_TAG not in self.iptcKeys():
49 return None
50 return self[NAME_TAG]
52 def get_tags(self,tagdelim=None):
53 if TAGS_TAG not in self.iptcKeys():
54 return None
55 tags = self[TAGS_TAG]
56 if tagdelim != None and type(tags) != str:
57 return tagdelim.join(tags)
58 return tags
60 def get_desc(self):
61 if DESC_TAG not in self.iptcKeys():
62 return None
63 return self[DESC_TAG]
65 def get_date(self):
66 if DATE_TAG not in self.exifKeys():
67 return None
68 return self[DATE_TAG]
70 def set_name(self,name):
71 self[NAME_TAG] = name
73 def set_tags(self,tags,string_sep=" "):
74 if type(tags) == str:
75 tags = tags.split(string_sep)
76 print tags
77 self[TAGS_TAG] = tags
79 def set_desc(self,desc):
80 self[DESC_TAG] = desc
82 def set_date(self,date):
83 self[DATE_TAG] = date
85 def get_of_type(self,t,tagdelim=None):
86 if t == TYPE_NAME:
87 return self.get_name()
88 if t == TYPE_DESC:
89 return self.get_desc()
90 if t == TYPE_TAGS:
91 return self.get_tags(tagdelim)
93 def set_of_type(self,t,x,tagdelim=" "):
94 if t == TYPE_NAME:
95 return self.set_name(x)
96 if t == TYPE_DESC:
97 return self.set_desc(x)
98 if t == TYPE_TAGS:
99 return self.set_tags(x,tagdelim)
101 def get_width(self):
102 return self[WIDTH_TAG]
103 def get_height(self):
104 return self[HEIGHT_TAG]
106 def write(self):
107 self.writeMetadata()
109 # todo: different sanitize functions for everything and get rid of space char and excludes and join.
110 def strftime_plus(self,s,name_spacechar=None,tags_join=" ",tags_spacechar=None,tags_excludes=[], \
111 desc_spacechar=None, sanitize_func=None):
112 """Similar to strftime, replaces, in the string argument, sequences like
113 %%d, %Y, %m with the year, month and date from the photo. Also replaces the
114 following non-date-related % sequences:
116 %n name, with spaces converted to arg spacechar if given
117 %t tags, except those in arg tags_excludes, with tags_spacechar
118 %s description, with spaces converted to arg desc_spacechar if given
119 %P original path
120 %F basename of original filename, minus suffix (no suffix or directory)
121 %E extension of original filename, such as ".JPG" or ".jpeg"
123 Note: you may have to switch "bad" characters from the output of this, since
124 name, tags, description could have newlines, tabs, or other "bad characters"."""
125 import datetime
127 # really should only calculate if needed.
128 # print "debug %s %s" % (self.get_date(),self.filename)
129 date = self.get_date()
130 name = self.get_name()
132 tags_list = self.get_tags()
133 if tags_list is None: tags_list = []
134 tags_list = [i for i in tags_list if i not in tags_excludes]
136 tags = tags_join.join(tags_list)
138 desc = self.get_desc()
140 if name is None: name = ""
141 if tags is None: tags = ""
142 if desc is None: desc = ""
144 if name_spacechar is not None:
145 name = name.replace(" ",name_spacechar)
146 if tags_spacechar is not None:
147 tags = tags.replace(" ",tags_spacechar)
148 if desc_spacechar is not None:
149 desc = desc.replace(" ",desc_spacechar)
151 if sanitize_func is not None:
152 name = sanitize_func(name)
153 tags = sanitize_func(tags)
154 desc = sanitize_func(desc)
156 s = s.replace("%n",name)
157 s = s.replace("%t",tags)
158 s = s.replace("%s",desc)
159 s = s.replace("%P",os.path.dirname(self.filename))
160 s = s.replace("%F",os.path.basename(os.path.splitext(self.filename)[0]))
161 s = s.replace("%E",os.path.splitext(self.filename)[1])
163 if date:
164 s = date.strftime(s)
165 return s
167 # if no date???