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