Question

I have a model called Picture . When an image is uploaded into this model , it will automatically be re-size before saving.

My main goal is to re-size the uploaded image into 2 separate images . So I can use it for different purpose like small picture , and big pictures . So what I done to achieve this goal is create another field called small . which represents small pictures .

I have 2 functions underneath my model called save and small . These functions will automatically re-size the image.

My plan is , when I upload an image to the model . My save function will automically resize the image and save it into images folder but how can I also get my small function to grab that image from image field so it can resize it and save it into my small field.

To sum is all up , it's just retrieveing an upload image and resizes the image on both field.

class Picture(models.Model):
    user = models.ForeignKey(User)
    small = models.ImageField(upload_to="small/",blank=True,null=True)
    image = models.ImageField(upload_to="images/",blank=True)

    def save(self , force_insert=False,force_update=False):
        super (Picture,self).save(force_insert,force_update)

        pw = self.image.width
        ph = self.image.height
        mw = 500
        mh = 500

        if (pw > mw) or (ph > mh):
            filename = str(self.image.path)
            imageObj = img.open(filename)
            ratio = 1

            if ( pw > mw):
                ratio = mw / float(pw)
                pw = mw
                ph = int(math.floor(float(ph)* ratio))
            if ( ph > mh):
                ratio = ratio * ( mh /float(ph))
                ph = mh 
                pw = int(math.floor(float(ph)* ratio))

            imageObj = imageObj.resize((pw,ph),img.ANTIALIAS)
            imageObj.save(filename)

    def save(self , force_insert=False,force_update=False):
        super (Picture,self).save(force_insert,force_update)

        pw = self.image.width
        ph = self.image.height
        mw = 300
        mh = 300

        if (pw > mw) or (ph > mh):
            filename = str(self.image.path)
            imageObj = img.open(filename)
            ratio = 1

            if ( pw > mw):
                ratio = mw / float(pw)
                pw = mw
                ph = int(math.floor(float(ph)* ratio))
            if ( ph > mh):
                ratio = ratio * ( mh /float(ph))
                ph = mh 
                pw = int(math.floor(float(ph)* ratio))

            imageObj = imageObj.resize((pw,ph),img.ANTIALIAS)
            imageObj.save(filename)

If this doesn't make sense , alert me so I can modify it

Was it helpful?

Solution

You could create a custom field (inheriting ImageField) or create a pre_save signal to process the upload.

from django.db.models.signals import pre_save
from django.dispatch import receiver
from myapp.models import MyModel


class MyModel(models.Model):
    # other fields
    image = MyCustomImageField(sizes=(('small', '300x300'), ('large', '500x500')))

A signal

@receiver(pre_save, sender=MyModel)
def process_picture(sender, **kwargs):
    # do resizing and storage stuff

More on signals and custom fields.

A working example of a custom ImageField.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top