How to resize and store pics with Django

212 views
Skip to first unread message

ivan77

unread,
Jun 3, 2016, 12:55:28 PM6/3/16
to Django users
HI,

I would like my Django App to have the functionality where a person can upload pictures (that would be resized).  

Can you please give me any advice on the best way to do that in Django.  I will be using a postgres DB for the app. 

Also, if there are any Python/Django apps that you can suggest, please let me know.

I found this on stack overflow which is some generic advice:



Store the pictures on the file system and picture locations in the database.

Why? Because...

  1. You will be able to serve the pictures as static files.
  2. No database access or application code will be required to fetch the pictures.
  3. The images could be served from a different server to improve performance.
  4. It will reduce database bottleneck.
  5. The database ultimately stores its data on the file system.
  6. Images can be easily cached when stored on the file system.


Thanks for any ideas,

Ivan


Ketan Bhatt

unread,
Jun 3, 2016, 1:01:28 PM6/3/16
to Django users
This is how you can compress the images while saving:

```python
import urllib2
import cStringIO
from boto3.session import Session
from PIL import Image

aws_session = Session(
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
region_name=settings.AWS_DEFAULT_LAMBDA_REGION
)
s3_resource = aws_session.resource('s3')
im = Image.open(cStringIO.StringIO(urllib2.urlopen(image_url).read()))

w, h = im.size
w *= 0.5
h *= 0.5

im.thumbnail((w, h))
buf = cStringIO.StringIO()
im.save(buf, format="JPEG")

s3_resource.Object(bucket, filename).put(Body=buf.getvalue(), ACL='public-read')
```

I am reading images from a URL and compressing them and then saving to S3 using boto. 

See how and where you want to store your images and just change the required line.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/530227c6-e9ad-4163-a15d-5afa9eab880a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ivan77

unread,
Jun 3, 2016, 1:23:40 PM6/3/16
to Django users
Hi Ketan,

Thanks for the information!

How is it that you keep the images associated with other tables in the database?  

What I am specifically thinking about is if you want to associate specific images with specific users who uploaded, for example?  Some kind of bridge (many to many table)?

Ivan

Ketan Bhatt

unread,
Jun 3, 2016, 2:08:01 PM6/3/16
to Django users
I am storing links of these images as I need to show them on the frontend, not connected to any user.

But I have done what you are talking about too. 
So Django gives you a Model File Field. Add that to your Model. That will allow you to save and store files. While saving a model object, just use the above code to compress the images.

You can find about FileField and how to use it from here https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.FileField



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

ivan77

unread,
Jun 3, 2016, 4:07:34 PM6/3/16
to Django users
Thanks! 

monoBOT

unread,
Jun 3, 2016, 4:33:43 PM6/3/16
to django...@googlegroups.com
You should take a look to this plugin ... does exactly what you need. 


the user uploads the image and you create as many thumbnails for thoses images as needed


For more options, visit https://groups.google.com/d/optout.



--
monoBOT
Visite mi sitio(Visit my site): monobotsoft.es/blog/
Reply all
Reply to author
Forward
0 new messages