2011-07-18 74 views
2
def upload_path_handler(instance, filename): 
    return filename 

class SpectacleGallery(models.Model): 
    image = models.ImageField(upload_to=upload_path_handler) 
    def save(self, *args, **kwargs): 
     Image.open(self.image) 
     super(SpectacleGallery, self).save(*args, **kwargs) 

當我嘗試打開它,我得到:Django的 - 不能在模型打開的圖像保存()

IOError at /admin/index/spectacle/1/ 
cannot identify image file 

爲什麼?文件是一個適當的圖像。 是否在保存methos該文件不是PIL一個很好的格式?

編輯: 這裏是我的代碼最終工作的版本:

def save(self, *args, **kwargs): 
     # set paths and additional variables 
     self_pk = self.pk 
     spectacle_id = self.spectacle_id 
     spectacle_id_str = str(spectacle_id) 
     create_gallery_spectacle_dir(spectacle_id) 

     new_filename = generate_image_name_hash() 
     new_filename_main = new_filename + '.jpg' 
     new_filename_thumb = new_filename + '_thumb.jpg' 
     new_file_thumb_path = settings.SPECTACLE_GALLERY_UPLOAD_DIR + '/' + spectacle_id_str + '/' + new_filename_thumb 
     new_file_thumb_root_path = settings.SPECTACLE_GALLERY_UPLOAD_PATH + spectacle_id_str + '/' + new_filename_thumb 
     new_file_root_path = settings.SPECTACLE_GALLERY_UPLOAD_PATH + spectacle_id_str + '/' + new_filename_main 

     if self.image: 
      #set new name and thum name 
      self.image.name = settings.SPECTACLE_GALLERY_UPLOAD_DIR + '/' + spectacle_id_str + '/' + new_filename_main 
      self.image_thumb = new_file_thumb_path 

     # image is in form and action is add call the "real" save() method. 
     if self.image and not self_pk: 
      super(SpectacleGallery, self).save(*args, **kwargs) 

     # image is in form and action is edit: get old image info, create variable with image field 
     if self.image and self_pk: 
      old_img = SpectacleGallery.objects.get(pk=self.pk) 
      old_img_instance = old_img.image 

     if self.image: 
      if self_pk: 
       image = old_img_instance 
      else: 
       image = self.image 

     super(SpectacleGallery, self).save(*args, **kwargs) #Call the "real" save() method. 

     #if image in form 
     if self.image: 
      # open file with PIL and convert to RGB 
      tmp_file = Image.open(self.image.path) 
      if tmp_file.mode != 'RGB': 
       tmp_file = tmp_file.convert('RGB') 

      #create and save thumbnail 
      tmp_file.thumbnail(settings.SPECTACLE_GALLERY_THUMB_SIZE, Image.ANTIALIAS) #make thumbnail 
      tmp_file.save(new_file_thumb_root_path, 'JPEG') #save thumbnail 

      # if edit delete old images 
      if self_pk: 
       delete_image_and_thumb(old_img.image, old_img.image_thumb) 
      #open and resize original image 
      image = Image.open(self.image.path) 
      if image.mode != 'RGB': 
       image = image.convert('RGB') 
      image.thumbnail(settings.SPECTACLE_GALLERY_IMAGE_SIZE, Image.ANTIALIAS) #make thumbnail 
      image.save(new_file_root_path,'JPEG', quality=100) 

回答

3

Django的ImageField的是不是你需要做這樣的事情的圖像。

Image.open(self.image.path) 
+0

但是,當我添加一個新的文件,我仍然得到無法識別圖像文件的文件不存在有:/ – robos85

+0

所以圖像沒有保存到該位置?你有目錄上的寫權限嗎?此外,爲什麼您將upload_to設置爲upload_path_handler?嘗試刪除一秒鐘,看看是否有幫助。 –

+0

我用它來返回唯一的文件名。我想要根據foreign_key id(包含在我的路徑中)完全更改名稱和位置來保存mt文件。所以,當上傳我想保存我的原始圖像到指定的路徑,調整它的大小和_thumb後綴保存拇指。 – robos85