Django - 无法在模型中打开图像save()

时间:2011-07-18 00:54:08

标签: django django-models python-imaging-library

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)

当我尝试打开它时,我得到:

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

为什么呢?文件是一个合适的图像。 保存方法中的文件不是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)

1 个答案:

答案 0 :(得分:3)

Django imageField不是你需要做这样的事情的图像。

Image.open(self.image.path)