How do I preserve EXIF data while uploading an image to a web service written in the Django REST framework?

1,104 views
Skip to first unread message

grzegorz g.

unread,
Mar 25, 2014, 5:47:10 AM3/25/14
to django-res...@googlegroups.com
I have a webservice (in Django REST Framework) and I want to store images with exif data. However after image upload I do not have original rich exif data.

I suspect that the problem is similar to this one:
Android: Image upload to web-service loses EXIF data

In brief: EXIF data is stripped during encode.
 
How should I approach to this problem in Django REST Framework?

Reinout van Rees

unread,
Mar 25, 2014, 6:31:58 AM3/25/14
to django-res...@googlegroups.com
On 25-03-14 10:47, grzegorz g. wrote:
> I have a webservice (in Django REST Framework) and I want to store
> images with exif data. However after image upload I do not have original
> rich exif data.
>
> I suspect that the problem is similar to this one:
> Android: Image upload to web-service loses EXIF data
> <http://stackoverflow.com/questions/18276440/android-image-upload-to-web-service-loses-exif-data>
>
> In brief: EXIF data is stripped during encode.
>
> How should I approach to this problem in Django REST Framework?

I suspect it is not really a django or a restframework problem. A
filefield in a django model will normally store the image file as-is on
the filesystem. The EXIF data should be preserved as the image isn't
modified.

The EXIF data is only stripped if the image is somehow transformed
("maximum image size is 800x600 pixels", for instance) by the code that
stores it in the database of filesystem.

Which Django app do you use to store your images? An existing app or
something you made yourself? Anyway, I'd look at what that app does in
models.py.


Reinout

--
Reinout van Rees http://reinout.vanrees.org/
rei...@vanrees.org http://www.nelen-schuurmans.nl/
"If you're not sure what to do, make something. -- Paul Graham"

grzegorz g.

unread,
Mar 26, 2014, 5:53:17 AM3/26/14
to django-res...@googlegroups.com
Hi Reinout, thanks for quick response :)

models.py file is almost not used - I need only ImageField to upload image. I get  image content in views.py:

 data =  self.request.FILES['image'].read()
 file_like = cStringIO.StringIO(data)
 img = PIL.Image.open(file_like)
 np_array = numpy.array(img) 

And then I make operations on numpy array - it was ok in my previous tasks, however when I want to get exif:

scipy.misc.imsave(filename, np_array)
tags = subprocess.check_output('exiftool %s'%filename, shell=True)
os.remove(filename)

I miss a lot of specific information like: Primary Platform, CMM Flags, Device Model and so on. 

I use curl:

curl -X POST -S -H 'Accept: application/json' -u "user:pass" -F "image=@basket.jpg;type=image/jpg" ip:port/exif/

I tried to change image/jpg to raw/binary and use FileField instead of ImageField in models.py - no results.

Reinout van Rees

unread,
Mar 26, 2014, 10:24:21 AM3/26/14
to django-res...@googlegroups.com
On 26-03-14 10:53, grzegorz g. wrote:
> models.py file is almost not used - I need only ImageField to upload
> image. I get image content in views.py:
>
> data = self.request.FILES['image'].read()
> file_like = cStringIO.StringIO(data)
> img = PIL.Image.open(file_like)
> np_array = numpy.array(img)
>
> And then I make operations on numpy array - it was ok in my previous
> tasks, however when I want to get exif:
>
> scipy.misc.imsave(filename, np_array)
> tags = subprocess.check_output('exiftool %s'%filename, shell=True)
> os.remove(filename)

You're converting the image to a numpy array, which basically means you
*only* extract the actual image data, not any (exif) metadata.

Next, with scipy you write out the numpy array to a file. The numpy
array only contains the actual image data, so the new file will also
only contain the actual image data.


If you want to get your hands on the exif data, you'll have to write
`file_like` to disk and run exiftool on that one.

grzegorz g.

unread,
Mar 27, 2014, 4:02:31 AM3/27/14
to django-res...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages