Search code examples
pythondjangopython-imaging-libraryexif

preserve exif data while resizing images


I am working in a mobile application that allows users to upload images. I process these images using a django backend.

I want to save resized image in server.

I create thumbnails of the image using PIL, but all exif data of the image is lost. I want to preserve the exif data in the resized image.

I am trying to use the gexiv2 library to copy the exif data of the original image and save it to the resized image:

exif = GExiv2.Metadata(file_path)

To presrve exif data, I save the resized image to the disk and gexiv2 uses this file path:

# exif of orginal image
exif = GExiv2.Metadata(file_path)

# exif of resized image
newExif = GExiv2.Metadata('img/512_c')

# save all exif data of orinal image to resized
for tag in exif.get_exif_tags():
    newExif[tag] = exif[tag]

# edit exif data - size 
newExif['Exif.Photo.PixelXDimension'] = str(im.size[0])
newExif['Exif.Photo.PixelYDimension'] = str(im.size[1]).

But my issue is, django gives me the client uploaded images either as a file path or as a buffer.

When I get the file path, I have no issue to get the exif data of the original image using gexiv2.

When I get an image buffer, I can't directly get the exif data using gexiv2, since gexiv2 needs the file path parameter to get the exif, so I want to save the image buffer to disk temporarily.

What is the best method to save the image buffer to disk?


Solution

  • Finally I got the solution, https://stackoverflow.com/a/11813684/658976 This confirms all images uploaded to server had file path (no buffered images).