Django-can';t在模型保存()中打开图像

Django-can';t在模型保存()中打开图像,django,django-models,python-imaging-library,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)

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

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)
为什么??文件是正确的图像。 保存方法中的文件是否不是PIL的好格式

编辑: 以下是我的最终工作版本代码:

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

Django imageField不是您需要执行类似操作的图像

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)

但是,当我添加新文件时,文件不存在,并且我仍然无法识别图像文件:/n所以图像没有保存到该位置?你对目录有写权限吗?另外,为什么要将upload_设置为upload_path_处理程序?尝试删除它一秒钟,看看是否有帮助。我使用它只返回文件名。我想根据外键id(包括在我的路径中)保存名称和位置完全更改的mt文件。因此,在上载时,我想将原始图像保存到指定路径,调整其大小并使用_thumb后缀保存thumb。@robos85,这似乎合理,但现在您只返回名称,函数需要返回一个包含名称的完全限定路径,以便它也知道在哪里上载文件。如果只给它一个名称,它可能会尝试将其保存到/仅使用该名称。有关更多信息,请参阅此链接。
Image.open(self.image.path)