python,复制exif数据从一个图像到另一个



我正在使用OpenCV和python读取图像。

img=cv2.imread('orig.jpg')

,修改图像后,再次保存。

cv2.imwrite('modi.jpg', img)

但是现在它的EXIF数据丢失了。

如何将EXIF从origin .jpg复制到'modi.jpg'.

I try EXIF 1.3.5

with open('orig.jpg', 'rb') as img_file:
img1 = Image(img_file)
with open('modi.jpg', 'wb') as new_image_file:
new_image_file.write(img1.get_file())

但是它也覆盖modi.jpg的图像数据。

EDIT:如果您只是想将EXIF从一个图像复制到另一个图像,一个简单的方法可能是:

from PIL import Image
# load old image and extract EXIF
image = Image.open('orig.jpg')
exif = image.info['exif']
# load new image
image_new = Image.open('modi.jpg')
image_new.save('modi_w_EXIF.jpg', 'JPEG', exif=exif)

我在这里找到了这个例子。我注意到这个方法将图像缩略图(不是图像信息!)复制到新文件中。您可能需要重新设置缩略图。

来自同一页面的另一个例子似乎更简单:

import jpeg
jpeg.setExif(jpeg.getExif('orig.jpg'), 'modi.jpg') 

我无法安装所需的模块并对其进行测试,但是这可能值得一试。

关于EXIF模块:

您将覆盖映像,因为img1不仅是EXIF数据,而且是整个映像。您必须执行以下步骤:

  1. 加载包含EXIF信息的图像并获取该信息
  2. 加载缺少该信息的新图像
  3. 覆盖新图像的EXIF
  4. 用EXIF保存新图像

类似于:

f_path_1 = "path/to/original/image/with/EXIF.jpg"
f_path_2 = "path/to/new/image/without/EXIF.jpg"
f_path_3 = "same/as/f_path_2/except/for/test/purposes.jpg"
from exif import Image as exIm # I imported it that way because PIL also uses "Image"
with open(f_path_1, "rb") as original_image:
exif_template = exIm(original_image)
if not exif_template.has_exif:
Warning("No EXIF data found for " + f_path_1)
tags = exif_template.list_all()
with open(f_path_2, "rb") as new_rgb_image:
exif_new = exIm(new_rgb_image)
for tag in tags:
try:
exec("exif_new." + tag + "=exif_template." + tag)
except:
pass
with open(f_path_3, "wb") as new_rgb_image:
new_rgb_image.write(exif_new.get_file())

注意:由于某些原因,EXIF只能从原始图像中写入一些标签,而不是所有标签。

最新更新