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
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
40 class Image(pyexiv2
.Image
):
43 pyexiv2
.Image
.__init
__(self
,f
)
45 pyexiv2
.Image
.readMetadata(self
)
48 if NAME_TAG
not in self
.iptcKeys():
52 def get_tags(self
,tagdelim
=None):
53 if TAGS_TAG
not in self
.iptcKeys():
56 if tagdelim
!= None and type(tags
) != str:
57 return tagdelim
.join(tags
)
61 if DESC_TAG
not in self
.iptcKeys():
66 if DATE_TAG
not in self
.exifKeys():
70 def set_name(self
,name
):
73 def set_tags(self
,tags
,string_sep
=" "):
75 tags
= tags
.split(string_sep
)
79 def set_desc(self
,desc
):
82 def set_date(self
,date
):
85 def get_of_type(self
,t
,tagdelim
=None):
87 return self
.get_name()
89 return self
.get_desc()
91 return self
.get_tags(tagdelim
)
93 def set_of_type(self
,t
,x
,tagdelim
=" "):
95 return self
.set_name(x
)
97 return self
.set_desc(x
)
99 return self
.set_tags(x
,tagdelim
)
102 return self
[WIDTH_TAG
]
103 def get_height(self
):
104 return self
[HEIGHT_TAG
]
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
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"."""
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])