Skip to content

Commit

Permalink
[feature] add copy_to_another_image()
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoHsiao1 committed Jan 23, 2024
1 parent 77ac478 commit bb0b72b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 24 deletions.
10 changes: 10 additions & 0 deletions pyexiv2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ def clear_icc(self):
def clear_thumbnail(self):
self.img.clear_thumbnail()

def copy_to_another_image(self, filename, encoding='utf-8', clean_before_copy=True,
exif=True, iptc=True, xmp=True,
comment=True, icc=True, thumbnail=True):
""" Copy metadata from one image to another image.
By default, it will clean the existing metadata in another image before copying.
"""
self.img.copy_to_another_image(filename.encode(encoding), clean_before_copy,
exif, iptc, xmp,
comment, icc, thumbnail)


class ImageData(Image):
"""
Expand Down
97 changes: 73 additions & 24 deletions pyexiv2/lib/exiv2api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,54 @@ class Image{
check_error_log();
}

void copy_to_another_image(const char *filename, bool clean_before_copy,
bool exif, bool iptc, bool xmp,
bool comment, bool icc, bool thumbnail)
{
// Open another image
auto another_image = Exiv2::ImageFactory::open(filename);
if (another_image.get() == 0)
throw Exiv2::Error(Exiv2::ErrorCode::kerErrorMessage, "Can not open this image.");

// Whether to preserve the existing metadata in another image
if (!clean_before_copy) {
another_image->readMetadata();
}

// Start copying
if (exif) {
another_image->setExifData((*img)->exifData());
}
if (iptc) {
another_image->setIptcData((*img)->iptcData());
}
if (xmp) {
another_image->setXmpPacket((*img)->xmpPacket());
}
if (comment) {
another_image->setComment((*img)->comment());
}
if (icc) {
Exiv2::DataBuf buf = (*img)->iccProfile();
another_image->setIccProfile(std::move(buf));
}
if (thumbnail) {
Exiv2::ExifThumb exifThumb(another_image->exifData());
Exiv2::DataBuf buf = exifThumb.copy();
exifThumb.setJpegThumbnail(buf.c_data(), buf.size());
}

// Save another image
another_image->writeMetadata();
check_error_log();
}

// void copy_to_another_image(Buffer buffer, bool clean_before_copy)
// {
// *img = Exiv2::ImageFactory::open((Exiv2::byte *)buffer.data, buffer.size);
// if (img->get() == 0)
// throw Exiv2::Error(Exiv2::ErrorCode::kerErrorMessage, "Can not open this image.");
// }
};

py::object convert_exif_to_xmp(py::list table, py::str encoding)
Expand Down Expand Up @@ -597,30 +645,31 @@ PYBIND11_MODULE(exiv2api, m)
py::class_<Image>(m, "Image")
.def(py::init<const char *>())
.def(py::init<Buffer &>())
.def("close_image" , &Image::close_image)
.def("get_bytes" , &Image::get_bytes)
.def("get_mime_type" , &Image::get_mime_type)
.def("get_access_mode" , &Image::get_access_mode)
.def("read_exif" , &Image::read_exif)
.def("read_iptc" , &Image::read_iptc)
.def("read_xmp" , &Image::read_xmp)
.def("read_raw_xmp" , &Image::read_raw_xmp)
.def("read_comment" , &Image::read_comment)
.def("read_icc" , &Image::read_icc)
.def("read_thumbnail" , &Image::read_thumbnail)
.def("modify_exif" , &Image::modify_exif)
.def("modify_iptc" , &Image::modify_iptc)
.def("modify_xmp" , &Image::modify_xmp)
.def("modify_raw_xmp" , &Image::modify_raw_xmp)
.def("modify_comment" , &Image::modify_comment)
.def("modify_icc" , &Image::modify_icc)
.def("modify_thumbnail" , &Image::modify_thumbnail)
.def("clear_exif" , &Image::clear_exif)
.def("clear_iptc" , &Image::clear_iptc)
.def("clear_xmp" , &Image::clear_xmp)
.def("clear_comment" , &Image::clear_comment)
.def("clear_icc" , &Image::clear_icc)
.def("clear_thumbnail" , &Image::clear_thumbnail);
.def("close_image" , &Image::close_image)
.def("get_bytes" , &Image::get_bytes)
.def("get_mime_type" , &Image::get_mime_type)
.def("get_access_mode" , &Image::get_access_mode)
.def("read_exif" , &Image::read_exif)
.def("read_iptc" , &Image::read_iptc)
.def("read_xmp" , &Image::read_xmp)
.def("read_raw_xmp" , &Image::read_raw_xmp)
.def("read_comment" , &Image::read_comment)
.def("read_icc" , &Image::read_icc)
.def("read_thumbnail" , &Image::read_thumbnail)
.def("modify_exif" , &Image::modify_exif)
.def("modify_iptc" , &Image::modify_iptc)
.def("modify_xmp" , &Image::modify_xmp)
.def("modify_raw_xmp" , &Image::modify_raw_xmp)
.def("modify_comment" , &Image::modify_comment)
.def("modify_icc" , &Image::modify_icc)
.def("modify_thumbnail" , &Image::modify_thumbnail)
.def("clear_exif" , &Image::clear_exif)
.def("clear_iptc" , &Image::clear_iptc)
.def("clear_xmp" , &Image::clear_xmp)
.def("clear_comment" , &Image::clear_comment)
.def("clear_icc" , &Image::clear_icc)
.def("clear_thumbnail" , &Image::clear_thumbnail)
.def("copy_to_another_image" , &Image::copy_to_another_image);
m.def("convert_exif_to_xmp" , &convert_exif_to_xmp);
m.def("convert_iptc_to_xmp" , &convert_iptc_to_xmp);
m.def("convert_xmp_to_exif" , &convert_xmp_to_exif);
Expand Down

0 comments on commit bb0b72b

Please sign in to comment.