Django soft resize uploaded Image to multiple sizes and upload to respective folder

679 Views Asked by At

I have the following model on my App

def image_path(self, filename):
    return 'app_name/images/{}/{}'.format(slugify(self.name), filename)
        
class Color(models.Model):
    name = CICharField(max_length=22, unique=True)
    image = models.ImageField("Image", upload_to=image_path, blank=True)

Once the image is uploaded I want to create 3 images size: small (100 by 100), medium (300 by 300) and large (800 by 800) with the soft image crop. Then I want to manage the URL Structure for my upload.

For Eg.

Original Image URL from "Image" field = 'app_name/images/image_1.jpg' if I upload the image, Then it will produce the following images.

small = 'app_name/images/small/image_1.jpg'
medium = 'app_name/images/medium/image_1.jpg'
large = 'app_name/images/large/image_1.jpg'

Can anyone tell me, How can I achieve this on Django.

Thanks.

1

There are 1 best solutions below

0
On

To do image processing you better use OpenCV or Pillow or Numpy. Maybe I'm wrong but Django should work ok with OpenCV based scripts.

Here you have some basic operations with OpenCV.

Here you have how to save processed images in django (similar as you post).

Here some resizing pictures code using OpenCV.