How can I insert EXIF/other metadata into a JPEG stored in a memory buffer?

六眼飞鱼酱① 提交于 2020-06-25 05:13:49

问题


I have created a JPEG using Python OpenCV, EXIF data being lost in the process and apparently not being able to be re-added when calling imwrite (reference: Can't keep image exif data when editing it with opencv in python).

Two questions:

  1. In general, how can I write the original EXIF data/new custom metadata into a JPEG that exists in memory rather than a file?

  2. Would pillow/PIL be able to maintain the EXIF data and allow supplementary metadata to be added? As of 2013 (reference: how maintain exif data of images resizes using PIL) this did not seem possible except via a tmp file (which is not an option for me).

Thanks as ever


回答1:


I'm not certain I understand what you are trying to do, but I think you are trying to process an image with OpenCV and then re-insert the EXIF data you lost when OpenCV opened it...

So, hopefully you can do what you are already doing, but also open the image with PIL/Pillow and extract the EXIF data and then write it into the image processed by OpenCV.

from PIL import Image
import io

# Read your image with EXIF data using PIL/Pillow
imWithEXIF = Image.open('image.jpg')

You will now have a dict with the EXIF info in:

imWIthEXIF.info['exif']

You now want to write that EXIF data into your image you processed with OpenCV, so:

# Make memory buffer for JPEG-encoded image
buffer = io.BytesIO()

# Convert OpenCV image onto PIL Image
OpenCVImageAsPIL = Image.fromarray(OpenCVImage)

# Encode newly-created image into memory as JPEG along with EXIF from other image
OpenCVImageAsPIL.save(buffer, format='JPEG', exif=imWIthEXIF.info['exif']) 

Beware... I am assuming in the code above, that OpenCVImage is a Numpy array and that you have called cvtColor(cv2.COLOR_BGR2RGB) to go to the conventional RGB channel ordering that PIL uses rather than OpenCV's BGR channel ordering.

Keywords: Python, OpenCV, PIL, Pillow, EXIF, preserve, insert, copy, transfer, image, image processing, image-processing, dict, BytesIO, memory, in-memory, buffer.



来源:https://stackoverflow.com/questions/56699941/how-can-i-insert-exif-other-metadata-into-a-jpeg-stored-in-a-memory-buffer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!