1

I am trying upload images and than create an thumbnail of it and than store both in S3. After the file has been uploaded i am first uploading it to S3 and than trying to create thumbnail but it doesn't work as than PIL is not able to recognise the image. And secondly if I create the thumbnail first than while uploading original image I get EOF.

I think Django allows just once for the uploaded files to be used only once....Please kindly tell me a way to do so....Thanks in advance

Saransh Mohapatra
  • 9,430
  • 10
  • 39
  • 50
  • What's not working? How are you uploading to S3? If you have access to the file object, I don't see what's stopping you from performing multiple actions on it. Just seek(0) on the file so that future actions can correctly read() the file. There's also django-storages, but I think it struggles in these scenarios as its forced to upload/download multiple times. Client -> Django -> S3 -> Django -> S3. – Yuji 'Tomita' Tomita Dec 14 '12 at 11:06
  • "it doesn't work as than PIL is not able to recognise the image" How do you know that? – Rickard Zachrisson Dec 14 '12 at 11:13

3 Answers3

2

Uhm, you need to be more specific with your question, but we're doing the same thing and the workflow is as follows:

1) You get the file handle on file upload from request.FILES and store it somewhere on your local filesystem, so you don't work on stream -- which is what i would guess is causing your problems

2) You use PIL (or better yet, Pillow) to manipulate the image on the FS, do resizing, thumbnailing, whatever.

3) You use Boto (http://boto.cloudhackers.com/en/latest/) to upload to S3, because Boto takes the handling of AWS out of your hands.

It's quite straightforward and works well

Tomáš Plešek
  • 1,482
  • 2
  • 12
  • 21
  • Yeah.I think the problem is that I am working on the stream...But will storing the file locally and than working and uploading later not slow down the whole process?? – Saransh Mohapatra Dec 14 '12 at 15:35
  • And isn't there any way to be able to work with stream itself – Saransh Mohapatra Dec 14 '12 at 15:35
  • Well, a stream is a stream, so you either copy it, or you just make two calls to pil.thumbnail without using open twice, which could work, but I'm not sure. As for slowing down the process, yes it will a little and there is about hundred ways how to go about that, if it is even a concern (if this is in admin pages, then screw the dealy, if it's on frontend and you have hundreds of people uploading images, then use django-celery or something of that sort) – Tomáš Plešek Dec 14 '12 at 15:57
0

Did your PIL installation find the correct libraries when compiling. You might try uninstalling it and reinstalling it with pip.

Look towards the end of the compilation output and there is a section detailing which libraries are available.

I spent a long time trying to find out why it wasn't discovering my jpeg, png/zip libraries before realising it was looking in the wrong places.

In Debian, I needed to download the PIL/Pillow source and add:

_add_directory(library_dirs, "/usr/lib/x86_64-linux-gnu")

to the 'standard locations' section of the settings.py file - this is the directory my libjpeg.so was installed to

I then needed to run

python setup.py install

and check the output to make sure it had found the right library

Hope this helps

collend
  • 156
  • 1
  • 3
0

I finally figured it out. The problem was that it was a stream that the uploaded file is stored into, so everytime i read the file it would reach the EOF.

The only and best way out is to seek(0) everytime we read the file.

This is also needed when playing with other files also in django.

Saransh Mohapatra
  • 9,430
  • 10
  • 39
  • 50