diff --git a/doc/changes.rst b/doc/changes.rst index f781769..9541bbc 100644 --- a/doc/changes.rst +++ b/doc/changes.rst @@ -1,6 +1,11 @@ Changelog ========= +1.0.11 +------ + +- Add option argument to "load". + 1.0.10 ------ diff --git a/doc/functions.rst b/doc/functions.rst index a116f33..2e3aae3 100644 --- a/doc/functions.rst +++ b/doc/functions.rst @@ -9,7 +9,7 @@ Functions load ---- -.. py:function:: piexif.load(filename) +.. py:function:: piexif.load(filename, key_is_name=False) Return exif data as dict. Keys(IFD name), be contained, are "0th", "Exif", "GPS", "Interop", "1st", and "thumbnail". Without "thumbnail", the value is dict(tag/value). "thumbnail" value is JPEG as bytes. diff --git a/piexif/__init__.py b/piexif/__init__.py index b62ff1a..7b68e65 100644 --- a/piexif/__init__.py +++ b/piexif/__init__.py @@ -6,4 +6,4 @@ from ._exif import * -VERSION = '1.0.10' +VERSION = '1.0.11' diff --git a/piexif/_load.py b/piexif/_load.py index 0e2fc2f..98f6eb9 100644 --- a/piexif/_load.py +++ b/piexif/_load.py @@ -7,7 +7,7 @@ LITTLE_ENDIAN = b"\x49\x49" -def load(input_data): +def load(input_data, key_is_name=False): """ py:function:: piexif.load(filename) @@ -55,6 +55,9 @@ def load(input_data): exif_dict["1st"][ImageIFD.JPEGInterchangeFormatLength]) thumb = exifReader.tiftag[exif_dict["1st"][ImageIFD.JPEGInterchangeFormat]:end] exif_dict["thumbnail"] = thumb + + if key_is_name: + exif_dict = _get_key_name_dict(exif_dict) return exif_dict @@ -215,3 +218,15 @@ def convert_value(self, val): return data[0] else: return data + + +def _get_key_name_dict(exif_dict): + new_dict = { + "0th":{TAGS["Image"][n]["name"]:value for n, value in exif_dict["0th"].items()}, + "Exif":{TAGS["Exif"][n]["name"]:value for n, value in exif_dict["Exif"].items()}, + "1st":{TAGS["Image"][n]["name"]:value for n, value in exif_dict["1st"].items()}, + "GPS":{TAGS["GPS"][n]["name"]:value for n, value in exif_dict["GPS"].items()}, + "Interop":{TAGS["Interop"][n]["name"]:value for n, value in exif_dict["Interop"].items()}, + "thumbnail":exif_dict["thumbnail"], + } + return new_dict \ No newline at end of file diff --git a/tests/s_test.py b/tests/s_test.py index c394973..b4dce02 100644 --- a/tests/s_test.py +++ b/tests/s_test.py @@ -162,6 +162,29 @@ def test_load_from_pilImage_property(self): o.seek(0) Image.open(o).close() + def test_load_name_dict(self): + thumbnail_io = io.BytesIO() + thumb = Image.open(INPUT_FILE2) + thumb.thumbnail((40, 40)) + thumb.save(thumbnail_io, "JPEG") + thumb.close() + thumb_data = thumbnail_io.getvalue() + exif_dict = {"0th":ZEROTH_IFD, + "Exif":EXIF_IFD, + "GPS":GPS_IFD, + "Interop":INTEROP_IFD, + "1st":FIRST_IFD, + "thumbnail":thumb_data} + exif_bytes = piexif.dump(exif_dict) + im = Image.new("RGBA", (80, 80)) + + o = io.BytesIO() + im.save(o, format="jpeg", exif=exif_bytes) + im.close() + o.seek(0) + exif = piexif.load(o.getvalue(), True) + print(exif) + # dump ------ def test_no_exif_dump(self): o = io.BytesIO()