Source

lazygal / devscripts / copy-metadata

#!/usr/bin/env python

# Lazygal, a static web gallery generator.
# Copyright (C) 2011 Alexandre Rossi <alexandre.rossi@gmail.com>
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))


from gi.repository import GExiv2


def copy_metadata(src, dst):
    source_image = GExiv2.Metadata(src)
    dest_image = GExiv2.Metadata(dst)

    # Save dimensions
    if 'Exif.Photo.PixelXDimension' in dest_image \
    and 'Exif.Photo.PixelYDimension' in dest_image:
        dst_width = dest_image["Exif.Photo.PixelXDimension"]
        dst_height = dest_image["Exif.Photo.PixelYDimension"]
        has_dims = True
    else:
        has_dims = False

    for tag in source_image.get_exif_tags():
        dest_image[tag] = source_image[tag]

    if has_dims:
        # set EXIF image size info to resized size
        dest_image["Exif.Photo.PixelXDimension"] = dst_width
        dest_image["Exif.Photo.PixelYDimension"] = dst_height

    dest_image.save_file()


if __name__ == "__main__":
    copy_metadata(sys.argv[1], sys.argv[2])


# vim: ts=4 sw=4 expandtab