From b43439d0b4dcd3659d941642c79e6837d4330a64 Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Thu, 20 May 2010 16:53:49 +0200 Subject: Thin wrapper on top of libexiv2python.Preview, for a better encapsulation. --- src/exiv2wrapper_python.cpp | 31 ++++++------------ src/pyexiv2/__init__.py | 1 + src/pyexiv2/metadata.py | 3 +- src/pyexiv2/preview.py | 76 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 src/pyexiv2/preview.py diff --git a/src/exiv2wrapper_python.cpp b/src/exiv2wrapper_python.cpp index faa9b81..e5855c4 100644 --- a/src/exiv2wrapper_python.cpp +++ b/src/exiv2wrapper_python.cpp @@ -96,28 +96,15 @@ BOOST_PYTHON_MODULE(libexiv2python) .def("_getLangAltValue", &XmpTag::getLangAltValue) ; - class_("Preview", - "A preview image (properties and data buffer) embedded in " - "image metadata.", - init()) - - .def_readonly("mime_type", &Preview::_mimeType, - "The mime type of the preview image (e.g. 'image/jpeg')") - .def_readonly("extension", &Preview::_extension, - "The file extension of the preview image with a leading " - "dot (e.g. '.jpg')") - .def_readonly("size", &Preview::_size, - "The size of the preview image in bytes") - .def_readonly("dimensions", &Preview::_dimensions, - "A tuple containing the width and height of the preview " - "image in pixels") - .def_readonly("data", &Preview::_data, - "The preview image data buffer") - - .def("write_to_file", &Preview::writeToFile, - "Write the preview image to a file on disk.\n" - "The file extension will be automatically appended to the path\n" - "passed in parameter.") + class_("_Preview", init()) + + .def_readonly("mime_type", &Preview::_mimeType) + .def_readonly("extension", &Preview::_extension) + .def_readonly("size", &Preview::_size) + .def_readonly("dimensions", &Preview::_dimensions) + .def_readonly("data", &Preview::_data) + + .def("write_to_file", &Preview::writeToFile) ; class_("_Image", init()) diff --git a/src/pyexiv2/__init__.py b/src/pyexiv2/__init__.py index a1cc8da..2fbddc8 100644 --- a/src/pyexiv2/__init__.py +++ b/src/pyexiv2/__init__.py @@ -63,6 +63,7 @@ from pyexiv2.metadata import ImageMetadata from pyexiv2.exif import ExifValueError, ExifTag from pyexiv2.iptc import IptcValueError, IptcTag from pyexiv2.xmp import XmpValueError, XmpTag +from pyexiv2.preview import Preview from pyexiv2.utils import FixedOffset, Rational, NotifyingList, \ undefined_to_string, string_to_undefined, \ GPSCoordinate diff --git a/src/pyexiv2/metadata.py b/src/pyexiv2/metadata.py index 5cbfeb5..c275a9d 100644 --- a/src/pyexiv2/metadata.py +++ b/src/pyexiv2/metadata.py @@ -37,6 +37,7 @@ import libexiv2python from pyexiv2.exif import ExifTag from pyexiv2.iptc import IptcTag from pyexiv2.xmp import XmpTag +from pyexiv2.preview import Preview class ImageMetadata(object): @@ -299,7 +300,7 @@ class ImageMetadata(object): def previews(self): """List of the previews available in the image, sorted by increasing size.""" - return self._image._previews() + return [Preview(preview) for preview in self._image._previews()] def copy(self, other, exif=True, iptc=True, xmp=True): """ diff --git a/src/pyexiv2/preview.py b/src/pyexiv2/preview.py new file mode 100644 index 0000000..ad50409 --- /dev/null +++ b/src/pyexiv2/preview.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- + +# ****************************************************************************** +# +# Copyright (C) 2010 Olivier Tilloy +# +# This file is part of the pyexiv2 distribution. +# +# pyexiv2 is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# pyexiv2 is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with pyexiv2; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA. +# +# Author: Olivier Tilloy +# +# ****************************************************************************** + +""" +Provide the Preview class. +""" + +class Preview(object): + + """ + A preview image (properties and data buffer) embedded in image metadata. + """ + + def __init__(self, preview): + self.__preview = preview + + @property + def mime_type(self): + """The mime type of the preview image (e.g. ``image/jpeg``).""" + return self.__preview.mime_type + + @property + def extension(self): + """"The file extension of the preview image with a leading dot + (e.g. ``.jpg``).""" + return self.__preview.extension + + @property + def size(self): + """The size of the preview image in bytes.""" + return self.__preview.size + + @property + def dimensions(self): + """A tuple containing the width and height of the preview image + in pixels.""" + return self.__preview.dimensions + + @property + def data(self): + """The preview image data buffer.""" + return self.__preview.data + + def write_to_file(self, path): + """ + Write the preview image to a file on disk. + The file extension will be automatically appended to the path. + + :param path: path to write the preview to (without an extension) + :type path: string + """ + return self.__preview.write_to_file(path) + -- cgit