>> There is more problem with exif comment : the string can be not ascii. >> The first character of the string indicate the charset [1], it should be >> parsed and removed of the final string. >> In order genshi template works we need to convert the string to unicode [2]. >> >> >> [1] charset="Unicode" Marseille : CathÃ(c)drale Sainte-Marie-Majeure >> [2] >> http://genshi.edgewall.org/wiki/GenshiFaq#WhydoesGenshiraiseaUnicodeDecodeErrorwhenItrytorendernon-ASCIIstrings >> >> PS : lazygal don't support old exif comment Exif.Image.ImageDescription >> http://www.exiv2.org/tags.html > > Can you please send me some sample image with both these problems?
Yes this would be nice, as I've been struggling to generate test material for the fix I've been working on (first problem only, UserComment encoding). My current patch : --- old-lazygal/lazygal/metadata.py 2008-06-17 09:58:16.000000000 +0200 +++ new-lazygal/lazygal/metadata.py 2008-06-17 09:58:16.000000000 +0200 @@ -148,7 +148,7 @@ is not found. ''' try: - return str(self[name]).strip('\0') + return str(self[name]).strip('\0').strip(' ') except (IndexError, ValueError, KeyError): return '' @@ -193,8 +193,37 @@ if ret == '': raise ValueError else: - return ret - except (ValueError, KeyError): + tokens = ret.split(' ') + if tokens[0].startswith('charset='): + charset = tokens[0] + value = ' '.join(tokens[1:]) + dummy, charsetvalue = charset.split('=') + charset = charsetvalue.strip("\"") + else: + charset = None + value = ret + + if charset == 'Unicode': + # Some forums, namely + # http://www.adobeforums.com/webx/[EMAIL PROTECTED]@.3c0614b3 + # seem to say that Unicode in this tag means utf-16... + encoding = 'utf-16' + elif charset == 'Ascii': + encoding = 'ascii' + elif charset == 'Jis': + encoding = 'shift_jis' + else: + # utf-8 here because most Linux distributions default now + # to utf-8. latin-1 may also be a good choice, but not + # everywhere in the world... + encoding = 'utf-8' + + try: + print value.decode(encoding) + return value.decode(encoding) + except UnicodeDecodeError, ex: + raise ValueError(ex) + except (ValueError, KeyError), ex: return self.get_jpeg_comment() def get_flash(self): How i'm trying to generate test material : #!/usr/bin/env python import pyexiv2 if __name__ == '__main__': import sys charset = sys.argv[1] if charset == 'Unicode': encoding = 'utf-16' elif charset == 'Ascii': encoding = 'ascii' else: raise ValueError('%s unknown' % charset) charset = 'charset="%s"' % charset file = sys.argv[2] comment = sys.argv[3].decode('utf-8') img = pyexiv2.Image(file) img.readMetadata() img['Exif.Photo.UserComment'] = charset + ' ' + comment.encode(encoding) img.writeMetadata() Alex