Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django image resizing and convert before upload

I searched a lot on this subject but couldn't really find what I need. I'll explain my problem :

On my website, the user can upload an image. I need to resize this image and convert it to JPEG before uploading it.

I saw some solutions using a method in the Views.py, but I'd like to do it by overriding the save() method in my Models.py. The problem is, all solutions I found for resizing image in save method do it after saving it a first time (calling super fonction), which means it'd use bandwidth (if I use a CDN) for nothing (am I right on this point?).

Thank you for your help

like image 287
Raphael Laurent Avatar asked Jun 23 '14 18:06

Raphael Laurent


People also ask

How do I create a thumbnail image in Django?

2) How to create the thumbnails of Image ?.. In Django , have the 'PIL' Package which is used modify/resize/create thumbnail of image files, while saving to databse. That means we have to do code while saving the images to server in Models file of Django as in below.


1 Answers

First, it's best to establish the correct language. Django and Python exist only on the server side. Therefore, anything they manipulate, save, or otherwise use, has to be first sent to the server. If Django or Python is to manage the photo, the user MUST upload this photo to the server first. Once the photo is uploaded, Django is free to make changes before storing the file.

If your concern is with upload bandwidth, and you don't want large files being uploaded, you will have to resize and reformat the photo on the client side. If this is a web application, this can be done using Javascript, but can not be done with Python, since Python does not operate on the client side for an application like yours.

If your concern is not with bandwidth, then you're free to have the user "upload" the file, but then have Django resize and reformat it before saving.

You are correct that you will want to override your save function for the photo object. I would recommend using a library to handle the resizing and reformatting, such as sorl.

from sorl.thumbnail import ImageField, get_thumbnail

class MyPhoto(models.Model):
    image = ImageField()

    def save(self, *args, **kwargs):
        if self.image:
            self.image = get_thumbnail(self.image, '500x600', quality=99, format='JPEG')
        super(MyPhoto, self).save(*args, **kwargs)

Sorl is just a library I am confident and familiar with, but it takes some tuning and configuration. You can check out Pillow or something instead, and just replace the line overriding self.image.

I also found a similar question here.

Edit: saw the update to your comment response above. Also note that if your webserver is handling Django, and your files are being saved to some CDN, this method will work. The image will be resized on the webserver before being uploaded to your CDN (assuming your configuration is as I'm assuming).

Hope this helps!

like image 166
Jamie Counsell Avatar answered Oct 06 '22 23:10

Jamie Counsell