diff options
-rw-r--r-- | src/pyexiv2/exif.py | 19 | ||||
-rw-r--r-- | test/exif.py | 6 | ||||
-rw-r--r-- | test/usercomment.py | 16 |
3 files changed, 27 insertions, 14 deletions
diff --git a/src/pyexiv2/exif.py b/src/pyexiv2/exif.py index ec2c630..0e4d308 100644 --- a/src/pyexiv2/exif.py +++ b/src/pyexiv2/exif.py @@ -234,13 +234,18 @@ class ExifTag(ListenerInterface): elif charset == 'Jis': encoding = 'shift_jis' elif charset == 'Unicode': - byte_order = self._tag._getByteOrder() - if byte_order == 1: - # little endian (II) - encoding = 'utf-16le' - elif byte_order == 2: - # big endian (MM) - encoding = 'utf-16be' + # Starting from 0.20, exiv2 converts unicode comments to UTF-8 + from pyexiv2 import __exiv2_version__ + if __exiv2_version__ >= '0.20': + encoding = 'utf-8' + else: + byte_order = self._tag._getByteOrder() + if byte_order == 1: + # little endian (II) + encoding = 'utf-16le' + elif byte_order == 2: + # big endian (MM) + encoding = 'utf-16be' elif charset == 'Undefined': pass elif charset == 'InvalidCharsetId': diff --git a/test/exif.py b/test/exif.py index f1496e9..1f929b7 100644 --- a/test/exif.py +++ b/test/exif.py @@ -135,10 +135,10 @@ class TestExifTag(unittest.TestCase): tag = ExifTag('Exif.Photo.UserComment') self.assertEqual(tag.type, 'Comment') self.assertEqual(tag._convert_to_python('A comment'), 'A comment') - charsets = ('Ascii', 'Jis', 'Unicode', 'Undefined', 'InvalidCharsetId') - for charset in charsets: + for charset in ('Ascii', 'Jis', 'Unicode', 'Undefined', 'InvalidCharsetId'): self.assertEqual(tag._convert_to_python('charset="%s" A comment' % charset), 'A comment') - self.failIfEqual(tag._convert_to_python('charset="%s" déjà vu' % charset), 'déjà vu') + for charset in ('Ascii', 'Jis', 'Undefined', 'InvalidCharsetId'): + self.failIfEqual(tag._convert_to_python('charset="%s" déjà vu' % charset), u'déjà vu') def test_convert_to_string_comment(self): # Valid values diff --git a/test/usercomment.py b/test/usercomment.py index 72c9dbb..9104fa2 100644 --- a/test/usercomment.py +++ b/test/usercomment.py @@ -48,6 +48,14 @@ class TestUserCommentReadWrite(unittest.TestCase): m.read() return m + def _expected_raw_value(self, endianness, value): + from pyexiv2 import __exiv2_version__ + if __exiv2_version__ >= '0.20': + return value + else: + encodings = {'ii': 'utf-16le', 'mm': 'utf-16be'} + return value.decode('utf-8').encode(encodings[endianness]) + def test_read_ascii(self): m = self._read_image('usercomment-ascii.jpg') tag = m['Exif.Photo.UserComment'] @@ -57,13 +65,13 @@ class TestUserCommentReadWrite(unittest.TestCase): def test_read_unicode_little_endian(self): m = self._read_image('usercomment-unicode-ii.jpg') tag = m['Exif.Photo.UserComment'] - self.assertEqual(tag.raw_value, 'charset="Unicode" d\x00\xe9\x00j\x00\xe0\x00 \x00v\x00u\x00') + self.assertEqual(tag.raw_value, 'charset="Unicode" %s' % self._expected_raw_value('ii', 'déjà vu')) self.assertEqual(tag.value, u'déjà vu') def test_read_unicode_big_endian(self): m = self._read_image('usercomment-unicode-mm.jpg') tag = m['Exif.Photo.UserComment'] - self.assertEqual(tag.raw_value, 'charset="Unicode" \x00d\x00\xe9\x00j\x00\xe0\x00 \x00v\x00u') + self.assertEqual(tag.raw_value, 'charset="Unicode" %s' % self._expected_raw_value('mm', 'déjà vu')) self.assertEqual(tag.value, u'déjà vu') def test_write_ascii(self): @@ -84,14 +92,14 @@ class TestUserCommentReadWrite(unittest.TestCase): m = self._read_image('usercomment-unicode-ii.jpg') tag = m['Exif.Photo.UserComment'] tag.value = u'DÉJÀ VU' - self.assertEqual(tag.raw_value, 'charset="Unicode" D\x00\xc9\x00J\x00\xc0\x00 \x00V\x00U\x00') + self.assertEqual(tag.raw_value, 'charset="Unicode" %s' % self._expected_raw_value('ii', 'DÉJÀ VU')) self.assertEqual(tag.value, u'DÉJÀ VU') def test_write_unicode_big_endian(self): m = self._read_image('usercomment-unicode-mm.jpg') tag = m['Exif.Photo.UserComment'] tag.value = u'DÉJÀ VU' - self.assertEqual(tag.raw_value, 'charset="Unicode" \x00D\x00\xc9\x00J\x00\xc0\x00 \x00V\x00U') + self.assertEqual(tag.raw_value, 'charset="Unicode" %s' % self._expected_raw_value('mm', 'DÉJÀ VU')) self.assertEqual(tag.value, u'DÉJÀ VU') |