缩略图

时间:2015-11-06 05:23:05

标签: django thumbnails

我正在尝试使用我从https://gist.github.com/valberg/2429288

获得的代码创建缩略图

但我正面临这个错误

分配前引用的本地变量'PIL_TYPE'

我不知道在哪里追踪错误。

def create_thumbnail(self):
        # original code for this method came from
        # http://snipt.net/danfreak/generate-thumbnails-in-django-with-pil/

        # If there is no image associated with this.
        # do not create thumbnail
        if not self.image:
            return

        from PIL import Image
        from cStringIO import StringIO
        from django.core.files.uploadedfile import SimpleUploadedFile
        import os

        # Set our max thumbnail size in a tuple (max width, max height)
        THUMBNAIL_SIZE = (99, 66)

        DJANGO_TYPE = self.image.file.content_type

        if DJANGO_TYPE == 'image/jpeg':
            PIL_TYPE = 'jpeg'
            FILE_EXTENSION = 'jpg'
        elif DJANGO_TYPE == 'image/png':
            PIL_TYPE = 'png'
            FILE_EXTENSION = 'png'

        # Open original photo which we want to thumbnail using PIL's Image
        image = Image.open(StringIO(self.image.read()))

        # We use our PIL Image object to create the thumbnail, which already
        # has a thumbnail() convenience method that contrains proportions.
        # Additionally, we use Image.ANTIALIAS to make the image look better.
        # Without antialiasing the image pattern artifacts may result.
        image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)

        # Save the thumbnail
        temp_handle = StringIO()
        image.save(temp_handle, PIL_TYPE)
        temp_handle.seek(0)

        # Save image to a SimpleUploadedFile which can be saved into
        # ImageField
        suf = SimpleUploadedFile(os.path.split(self.image.name)[-1],
                temp_handle.read(), content_type=DJANGO_TYPE)
        # Save SimpleUploadedFile into image field
        self.thumbnail.save(
            '%s_thumbnail.%s' % (os.path.splitext(suf.name)[0], FILE_EXTENSION),
            suf,
            save=False
        )

    def save(self, *args, **kwargs):

        self.create_thumbnail()

        force_update = False

        # If the instance already has been saved, it has an id and we set 
        # force_update to True
        if self.id:
            force_update = True

        # Force an UPDATE SQL query if we're editing the image to avoid integrity exception
        super(Image, self).save(force_update=force_update)

2 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为如果没有IF ELIFDJANGO_TYPEPIL_TYPE FILE_EXTENSION语句就无法解释在定义它们之前调用 - 这是在赋值之前引用的另一种方式)。在这种情况下,修改你的代码以做某事。

例如:

    if DJANGO_TYPE == 'image/jpeg':
        PIL_TYPE = 'jpeg'
        FILE_EXTENSION = 'jpg'
    elif DJANGO_TYPE == 'image/png':
        PIL_TYPE = 'png'
        FILE_EXTENSION = 'png'
    else:
        PIL_TYPE = None
        FILE_EXTENSION = None

或者您甚至可以在return 'Error, no filetype defined!'块中执行else以防止代码进一步运行(推荐)。

答案 1 :(得分:0)

在您的代码中:

if DJANGO_TYPE == 'image/jpeg':
    PIL_TYPE = 'jpeg'
    FILE_EXTENSION = 'jpg'
elif DJANGO_TYPE == 'image/png':
    PIL_TYPE = 'png'
    FILE_EXTENSION = 'png

您需要定义else语句,例如DJANGO_TYPE not in ['image/jpeg', 'image/png']或设置PIL_TYPE, FILE_EXTENSION的默认值,因为这两个条件都无法满足。