2013-04-20 4 views
3

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) 

이 이해가되지 않는 경우, 그래서 저를 경고 나는 그것이

+2

왜 사용하지 [sorl-썸네일 (http://sorl-thumbnail.readthedocs.org/en/latest/examples.html) 대신? – Darwin

+0

@Darwin 그레이트 대답, – donkeyboy72

답변

1

당신은 (ImageField 상속) 사용자 정의 필드를 만들 수 있습니다 수정하거나 업로드를 처리하는 pre_save 신호를 생성 할 수 있습니다.

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'))) 

신호

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

더.

A working example of a custom ImageField.

+2

그건 당신이 만들려는 썸네일 크기를 정의하는 설정으로 사용되는 속성입니다. 섬네일을 만들려면 하나의 함수 만 있으면됩니다. 내가 게시 한 작업 예제를 살펴보십시오. –

+1

@Jacob, 정말 파이썬입니다. 장고는 단지 파이썬 코드를 사용합니다. [예제] (https://github.com/django-lfs/lfs/blob/master/core/fields/thumbs.py)는 꽤 잘 문서화되어 있습니다. 복사하여 붙여 넣으면 아마도 상자. 신중하게 읽고 당신은 기본적인 이해를해야합니다. –

+1

필드는 '단지'파이썬 클래스입니다. 예제의 [Line 97] (https://github.com/django-lfs/lfs/blob/master/core/fields/thumbs.py#L97)은 사용자 정의 필드를 정의합니다. –

관련 문제