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.
On Fri, Jun 3, 2016 at 10:25 PM ivan77 <[email protected]> wrote:
> 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:
>
> http://stackoverflow.com/a/561475/3856624
>
>
> 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.
> 7.
>
>
> Thanks for any ideas,
>
> Ivan
>
>
> --
> 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 [email protected].
> To post to this group, send email to [email protected].
> 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
> <https://groups.google.com/d/msgid/django-users/530227c6-e9ad-4163-a15d-5afa9eab880a%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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 [email protected].
To post to this group, send email to [email protected].
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/CACyRkBWCg%2BQdF2L_5mrXwxBRqzv_hCXNo73hu4sBt_XmW-kacg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.