From ec9105b690974b0246e36769506e735c4edf069a Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Wed, 1 Mar 2023 21:38:09 +0100 Subject: Exif data reader and extractor --- test/reader/test_exif.py | 48 +++++++++++++++++++++++++++++++++++++++++ test/reader/testimage_exif.jpg | Bin 0 -> 719 bytes 2 files changed, 48 insertions(+) create mode 100644 test/reader/test_exif.py create mode 100644 test/reader/testimage_exif.jpg (limited to 'test/reader') diff --git a/test/reader/test_exif.py b/test/reader/test_exif.py new file mode 100644 index 0000000..f1330da --- /dev/null +++ b/test/reader/test_exif.py @@ -0,0 +1,48 @@ +""" + +Part of the bsie test suite. +A copy of the license is provided with the project. +Author: Matthias Baumgartner, 2022 +""" +# standard imports +import os +import unittest + +# bsie imports +from bsie.utils import errors + +# objects to test +from bsie.reader.exif import Exif + + +## code ## + +class TestExif(unittest.TestCase): + def test_call(self): + rdr = Exif() + # discards non-image files + self.assertRaises(errors.UnsupportedFileFormatError, rdr, 'invalid.doc') + # raises on invalid image files + self.assertRaises(errors.ReaderError, rdr, 'invalid.jpg') + # returns dict with exif info + self.assertDictEqual(rdr(os.path.join(os.path.dirname(__file__), 'testimage_exif.jpg')), { + 'Exif.Image.Artist': 'nobody', + 'Exif.Image.ExifTag': '110', + 'Exif.Image.ResolutionUnit': '2', + 'Exif.Image.XResolution': '300/1', + 'Exif.Image.YCbCrPositioning': '1', + 'Exif.Image.YResolution': '300/1', + 'Exif.Photo.ColorSpace': '65535', + 'Exif.Photo.ComponentsConfiguration': '1 2 3 0', + 'Exif.Photo.ExifVersion': '48 50 51 50', + 'Exif.Photo.FlashpixVersion': '48 49 48 48', + 'Exif.Photo.ISOSpeedRatings': '200', + }) + + +## main ## + +if __name__ == '__main__': + unittest.main() + +## EOF ## diff --git a/test/reader/testimage_exif.jpg b/test/reader/testimage_exif.jpg new file mode 100644 index 0000000..a774bc2 Binary files /dev/null and b/test/reader/testimage_exif.jpg differ -- cgit v1.2.3 From 4b5c4d486bb4f0f4da2e25ad464e8336a781cdcb Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Wed, 1 Mar 2023 22:31:03 +0100 Subject: removed module header stubs --- test/reader/image/load_nef.py | 5 ----- test/reader/image/test_image.py | 5 ----- test/reader/image/test_pillow.py | 5 ----- test/reader/image/test_raw_image.py | 5 ----- test/reader/preview/load_nef.py | 5 ----- test/reader/preview/test_pg.py | 5 ----- test/reader/preview/test_pillow.py | 5 ----- test/reader/preview/test_preview.py | 5 ----- test/reader/preview/test_rawpy.py | 5 ----- test/reader/preview/test_utils.py | 5 ----- test/reader/test_base.py | 5 ----- test/reader/test_builder.py | 5 ----- test/reader/test_chain.py | 5 ----- test/reader/test_exif.py | 5 ----- test/reader/test_path.py | 5 ----- test/reader/test_stat.py | 5 ----- 16 files changed, 80 deletions(-) (limited to 'test/reader') diff --git a/test/reader/image/load_nef.py b/test/reader/image/load_nef.py index 5ba0adc..02be470 100644 --- a/test/reader/image/load_nef.py +++ b/test/reader/image/load_nef.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import os diff --git a/test/reader/image/test_image.py b/test/reader/image/test_image.py index 26f6a93..ee9b8f9 100644 --- a/test/reader/image/test_image.py +++ b/test/reader/image/test_image.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import importlib import os diff --git a/test/reader/image/test_pillow.py b/test/reader/image/test_pillow.py index 8abf5c1..2cff768 100644 --- a/test/reader/image/test_pillow.py +++ b/test/reader/image/test_pillow.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import os import unittest diff --git a/test/reader/image/test_raw_image.py b/test/reader/image/test_raw_image.py index ba21b5a..0a5ed63 100644 --- a/test/reader/image/test_raw_image.py +++ b/test/reader/image/test_raw_image.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import importlib import os diff --git a/test/reader/preview/load_nef.py b/test/reader/preview/load_nef.py index 5ba0adc..02be470 100644 --- a/test/reader/preview/load_nef.py +++ b/test/reader/preview/load_nef.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import os diff --git a/test/reader/preview/test_pg.py b/test/reader/preview/test_pg.py index e492cfa..381344f 100644 --- a/test/reader/preview/test_pg.py +++ b/test/reader/preview/test_pg.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports from functools import partial import os diff --git a/test/reader/preview/test_pillow.py b/test/reader/preview/test_pillow.py index ca38d89..d213bbb 100644 --- a/test/reader/preview/test_pillow.py +++ b/test/reader/preview/test_pillow.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports from functools import partial import os diff --git a/test/reader/preview/test_preview.py b/test/reader/preview/test_preview.py index fde610f..e144877 100644 --- a/test/reader/preview/test_preview.py +++ b/test/reader/preview/test_preview.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports from functools import partial import importlib diff --git a/test/reader/preview/test_rawpy.py b/test/reader/preview/test_rawpy.py index ed35f53..11a6f9b 100644 --- a/test/reader/preview/test_rawpy.py +++ b/test/reader/preview/test_rawpy.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports from functools import partial import importlib diff --git a/test/reader/preview/test_utils.py b/test/reader/preview/test_utils.py index c10c38c..2b15bc6 100644 --- a/test/reader/preview/test_utils.py +++ b/test/reader/preview/test_utils.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import os import unittest diff --git a/test/reader/test_base.py b/test/reader/test_base.py index 41f4c29..5dd2855 100644 --- a/test/reader/test_base.py +++ b/test/reader/test_base.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import unittest diff --git a/test/reader/test_builder.py b/test/reader/test_builder.py index 92e9edc..84e8e7a 100644 --- a/test/reader/test_builder.py +++ b/test/reader/test_builder.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import unittest diff --git a/test/reader/test_chain.py b/test/reader/test_chain.py index 901faa1..665aabc 100644 --- a/test/reader/test_chain.py +++ b/test/reader/test_chain.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import logging import os diff --git a/test/reader/test_exif.py b/test/reader/test_exif.py index f1330da..b2bf843 100644 --- a/test/reader/test_exif.py +++ b/test/reader/test_exif.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import os import unittest diff --git a/test/reader/test_path.py b/test/reader/test_path.py index 95e447f..f2eee06 100644 --- a/test/reader/test_path.py +++ b/test/reader/test_path.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import unittest diff --git a/test/reader/test_stat.py b/test/reader/test_stat.py index fd9fdcd..f36b8b3 100644 --- a/test/reader/test_stat.py +++ b/test/reader/test_stat.py @@ -1,9 +1,4 @@ -""" -Part of the bsie test suite. -A copy of the license is provided with the project. -Author: Matthias Baumgartner, 2022 -""" # standard imports import os import unittest -- cgit v1.2.3 From 70d77819a84c73292825b81f952e162bb30753d7 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Thu, 2 Mar 2023 08:56:57 +0100 Subject: reader error: untangle generic from unsupported format errors --- test/reader/image/test_raw_image.py | 4 ++-- test/reader/preview/test_pillow.py | 6 +++++- test/reader/test_exif.py | 13 +++++++++++-- test/reader/testimage_exif_corrupted.jpg | Bin 0 -> 551 bytes 4 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 test/reader/testimage_exif_corrupted.jpg (limited to 'test/reader') diff --git a/test/reader/image/test_raw_image.py b/test/reader/image/test_raw_image.py index 0a5ed63..3b240d0 100644 --- a/test/reader/image/test_raw_image.py +++ b/test/reader/image/test_raw_image.py @@ -32,10 +32,10 @@ class TestRawImage(unittest.TestCase): #self.assertEqual(img.getdata().getpixel((0, 0)), (0, 0, 0)) img.close() # raises exception when image cannot be read + self.assertRaises(errors.UnsupportedFileFormatError, rdr, + os.path.join(os.path.dirname(__file__), 'testimage.jpg')) self.assertRaises(errors.ReaderError, rdr, os.path.join(os.path.dirname(__file__), 'invalid.nef')) - self.assertRaises(errors.ReaderError, rdr, - os.path.join(os.path.dirname(__file__), 'testimage.jpg')) diff --git a/test/reader/preview/test_pillow.py b/test/reader/preview/test_pillow.py index d213bbb..20f08ec 100644 --- a/test/reader/preview/test_pillow.py +++ b/test/reader/preview/test_pillow.py @@ -20,11 +20,15 @@ class TestPillowPreviewReader(unittest.TestCase): def test_call(self): rdr = PillowPreviewReader() # raises exception when image cannot be read - self.assertRaises(errors.ReaderError, rdr, + self.assertRaises(errors.UnsupportedFileFormatError, rdr, os.path.join(os.path.dirname(__file__), 'invalid.jpg')) + # raises exception when image cannot be read + self.assertRaises(errors.ReaderError, rdr, + os.path.join(os.path.dirname(__file__), 'inexistent.jpg')) # raises exception when image has invalid type self.assertRaises(errors.UnsupportedFileFormatError, rdr, os.path.join(os.path.dirname(__file__), 'invalid.foo')) + # proper file produces a generator gen = rdr(os.path.join(os.path.dirname(__file__), 'testimage.jpg')) self.assertIsInstance(gen, partial) diff --git a/test/reader/test_exif.py b/test/reader/test_exif.py index b2bf843..de6e801 100644 --- a/test/reader/test_exif.py +++ b/test/reader/test_exif.py @@ -3,6 +3,9 @@ import os import unittest +# external imports +import pyexiv2 + # bsie imports from bsie.utils import errors @@ -16,9 +19,15 @@ class TestExif(unittest.TestCase): def test_call(self): rdr = Exif() # discards non-image files - self.assertRaises(errors.UnsupportedFileFormatError, rdr, 'invalid.doc') + self.assertRaises(errors.UnsupportedFileFormatError, rdr, + os.path.join(os.path.dirname(__file__), 'invalid.doc')) + # raises on invalid image files + self.assertRaises(errors.UnsupportedFileFormatError, rdr, + os.path.join(os.path.dirname(__file__), 'invalid.jpg')) # raises on invalid image files - self.assertRaises(errors.ReaderError, rdr, 'invalid.jpg') + pyexiv2.set_log_level(3) # suppress log message + self.assertRaises(errors.ReaderError, rdr, + os.path.join(os.path.dirname(__file__), 'testimage_exif_corrupted.jpg')) # returns dict with exif info self.assertDictEqual(rdr(os.path.join(os.path.dirname(__file__), 'testimage_exif.jpg')), { 'Exif.Image.Artist': 'nobody', diff --git a/test/reader/testimage_exif_corrupted.jpg b/test/reader/testimage_exif_corrupted.jpg new file mode 100644 index 0000000..e51a9dc Binary files /dev/null and b/test/reader/testimage_exif_corrupted.jpg differ -- cgit v1.2.3 From ba6329bbe14c832d42773dee2fe30bd7669ca255 Mon Sep 17 00:00:00 2001 From: Matthias Baumgartner Date: Thu, 2 Mar 2023 08:58:29 +0100 Subject: various minor fixes --- test/reader/preview/test_pg.py | 1 + 1 file changed, 1 insertion(+) (limited to 'test/reader') diff --git a/test/reader/preview/test_pg.py b/test/reader/preview/test_pg.py index 381344f..30095c5 100644 --- a/test/reader/preview/test_pg.py +++ b/test/reader/preview/test_pg.py @@ -52,6 +52,7 @@ class TestPreviewGeneratorReader(unittest.TestCase): self.assertEqual(sum(img.getdata()), 20258) # cleanup img.close() + del rdr # can define a cache dir pg_dir = tempfile.mkdtemp(prefix='bsie-test') -- cgit v1.2.3