Picture라는 모델이 있습니다. 이미지를이 모델에 업로드하면 저장하기 전에 이미지의 크기가 자동으로 조정됩니다.Django 다른 모델의 이미지 저장
내 주요 목표는 업로드 된 이미지를 2 개의 개별 이미지로 다시 크기 조정하는 것입니다. 그래서 저는 작은 그림과 큰 그림과 같은 다른 목적으로 사용할 수 있습니다. 그래서 제가이 목표를 달성하기 위해 한 것은 small이라는 또 다른 분야를 만드는 것입니다. 작은 그림을 나타냅니다.
나는 save와 small이라는 내 모델 아래에 2 개의 함수가 있습니다. 이 함수는 자동으로 이미지의 크기를 조정합니다.
내 계획은 모델에 이미지를 업로드 할 때입니다. 내 save 함수는 자동으로 이미지의 크기를 조정하여 이미지 폴더에 저장하지만 크기를 조정하고 작은 필드에 저장할 수 있도록 이미지 필드에서 해당 이미지를 가져 오는 작은 함수를 어떻게 얻을 수 있습니까?
합계가 올라간다. 단지 업로드 이미지를 검색하고 두 필드의 이미지 크기를 조정하는 것이다.
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)
이 이해가되지 않는 경우, 그래서 저를 경고 나는 그것이
왜 사용하지 [sorl-썸네일 (http://sorl-thumbnail.readthedocs.org/en/latest/examples.html) 대신? – Darwin
@Darwin 그레이트 대답, – donkeyboy72