Skip to content

Commit

Permalink
[lib] make copy_to_another_image() call Image instance
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoHsiao1 committed Jan 27, 2024
1 parent 103166d commit 0cddf62
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 60 deletions.
22 changes: 4 additions & 18 deletions pyexiv2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,17 @@ def clear_icc(self):
def clear_thumbnail(self):
self.img.clear_thumbnail()

def copy_to_another_image(self, filename: str, encoding='utf-8', clean_before_copy=True,
def copy_to_another_image(self, another_image,
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,
if not isinstance(another_image, (Image, ImageData)):
raise TypeError('The type of another_image should be pyexiv2.Image or pyexiv2.ImageData.')
self.img.copy_to_another_image(another_image.img,
exif, iptc, xmp,
comment, icc, thumbnail)

def copy_to_another_imageData(self, data: bytes, clean_before_copy=True,
exif=True, iptc=True, xmp=True,
comment=True, icc=True, thumbnail=True):
""" Copy metadata from one image to another image.
Opens another image from bytes data.
"""
length = len(data)
if length >= 2**31:
raise ValueError('Only images smaller than 2GB can be opened. The size of your image is {} bytes.'.format(length))
another_image_buffer = exiv2api.Buffer(data, length)
self.img.copy_to_another_imageData(another_image_buffer, clean_before_copy,
exif, iptc, xmp,
comment, icc, thumbnail)
another_image_buffer.destroy()


class ImageData(Image):
"""
Expand Down
53 changes: 11 additions & 42 deletions pyexiv2/lib/exiv2api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,65 +423,35 @@ class Image{
check_error_log();
}

void _copy_to_another_image(Exiv2::Image::UniquePtr another_image, bool clean_before_copy,
bool exif, bool iptc, bool xmp,
bool comment, bool icc, bool thumbnail)
void copy_to_another_image(Image& another_image,
bool exif, bool iptc, bool xmp,
bool comment, bool icc, bool thumbnail)
{
if (another_image.get() == 0)
throw Exiv2::Error(Exiv2::ErrorCode::kerErrorMessage, "Can not open this image.");

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

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

// Save another image
another_image->writeMetadata();
another_image.img->writeMetadata();
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)
{
Exiv2::Image::UniquePtr another_image = Exiv2::ImageFactory::open(filename);
this->_copy_to_another_image(std::move(another_image), clean_before_copy,
exif, iptc, xmp,
comment, icc, thumbnail);
}

void copy_to_another_imageData(Buffer buffer, bool clean_before_copy,
bool exif, bool iptc, bool xmp,
bool comment, bool icc, bool thumbnail)
{
Exiv2::Image::UniquePtr another_image = Exiv2::ImageFactory::open((Exiv2::byte *)buffer.data, buffer.size);
this->_copy_to_another_image(std::move(another_image), clean_before_copy,
exif, iptc, xmp,
comment, icc, thumbnail);
}
};

py::object convert_exif_to_xmp(py::list table, py::str encoding)
Expand Down Expand Up @@ -680,8 +650,7 @@ PYBIND11_MODULE(exiv2api, m)
.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)
.def("copy_to_another_imageData" , &Image::copy_to_another_imageData);
.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 0cddf62

Please sign in to comment.