Django-使用PIL在保存方法中即时调整图像大小并创建缩略图

时间:2019-04-15 07:49:37

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

我需要在 django 2.1.7 应用中调整图像大小并创建缩略图。 我决定以覆盖的SAVE方法通过PIL进行操作。

我现在有2个版本的代码,一个版本可以正常运行,而另一个版本则不能。 问题是我真的不知道发生了什么,因为我会说两个版本都应该工作。所以任何人都可以向我解释下面的代码在哪里:

#models.py
#I am getting error OSError: cannot identify image file <_io.BytesIO object at 0x105a10200>

from django.db import models
import os
import PIL
from PIL import Image
from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile

 class Photo(models.Model):
    image = models.ImageField(upload_to=change_image_name, null=True, blank=True, help_text = "")
    thumb = models.ImageField(upload_to=change_thumb_name, null=True, blank=True, help_text = "")
    gallery = models.ForeignKey(PhotoGallery, on_delete=models.CASCADE, related_name='image')



def save(self, *args, **kwargs):        
    maxsize = (1920, 1080) # max_size for image
    th_maxsize = (1000,1000) #ma_size for thubnail
    width,height = get_image_dimensions(self.image) 

    with Image.open(BytesIO(self.image.read())) as thumb:        
        if width > 1000:
            print ('image for thumb wider then 1000, doing resize')
            new_thumb = thumb.thumbnail(th_maxsize, PIL.Image.ANTIALIAS)
            output_1000 = BytesIO()
            thumb.save(output_1000, format='JPEG', quality=90)
            output_1000.seek(0)
            new_size = len(output_1000.getvalue())
            self.thumb = InMemoryUploadedFile(file=output_1000, field_name='ImageField', name="%s.jpg" % self.image.name.split('.')[0], content_type='image/jpeg', size=new_size, charset=None)
        else:
            print ('saving thumbnail, no resize')
            output_thumb = BytesIO()
            thumb.save(output_thumb, format='JPEG', quality='keep')
            output_thumb.seek(0)
            new_size = len(output_thumb.getvalue())
            self.thumb = InMemoryUploadedFile(file=output_thumb, field_name='ImageField', name="%s.jpg" % self.image.name.split('.')[0], content_type='image/jpeg', size=new_size, charset=None)

   if width > 1920:            
        with Image.open(BytesIO(self.image.read())) as img: #error appear here
            print ('image wider then 1920, doing resize')
            new_image = img.thumbnail(maxsize, PIL.Image.ANTIALIAS)
            output = BytesIO()
            img.save(output, format='JPEG', quality=90)
            output.seek(0)
            new_size = len(output.getvalue())
            self.image = InMemoryUploadedFile(file=output, field_name='ImageField', name="%s.jpg" % self.image.name.split('.')[0], content_type='image/jpeg', size=new_size, charset=None)             
    super(Photo, self).save(*args, **kwargs)

最后这是我只在有条件的情况下移动零件的代码

  

如果宽度> 1920

到顶部。这段代码非常完美,我不明白为什么第一个不行。有人可以解释这是怎么回事。

def save(self, *args, **kwargs):        
    maxsize = (1920, 1080) # max_size for image
    th_maxsize = (1000,1000) #ma_size for thubnail
    width,height = get_image_dimensions(self.image)

    if width > 1920:
        with Image.open(BytesIO(self.image.read())) as img:
            print ('image wider then 1920, doing resize')
            new_image = img.thumbnail(maxsize, PIL.Image.ANTIALIAS)
            output = BytesIO()
            img.save(output, format='JPEG', quality=90)
            output.seek(0)
            new_size = len(output.getvalue())
            self.image = InMemoryUploadedFile(file=output, field_name='ImageField', name="%s.jpg" % self.image.name.split('.')[0], content_type='image/jpeg', size=new_size, charset=None)        

    with Image.open(BytesIO(self.image.read())) as thumb:        
        if width > 1000:
            print ('image for thumb wider then 1000, doing resize')
            new_thumb = thumb.thumbnail(th_maxsize, PIL.Image.ANTIALIAS)
            output_1000 = BytesIO()
            thumb.save(output_1000, format='JPEG', quality=90)
            output_1000.seek(0)
            new_size = len(output_1000.getvalue())
            self.thumb = InMemoryUploadedFile(file=output_1000, field_name='ImageField', name="%s.jpg" % self.image.name.split('.')[0], content_type='image/jpeg', size=new_size, charset=None)
        else:
            print ('saving thubnail, no resize')
            output_thumb = BytesIO()
            thumb.save(output_thumb, format='JPEG', quality='keep')
            output_thumb.seek(0)
            new_size = len(output_thumb.getvalue())
            self.thumb = InMemoryUploadedFile(file=output_thumb, field_name='ImageField', name="%s.jpg" % self.image.name.split('.')[0], content_type='image/jpeg', size=new_size, charset=None)

    super(Photo, self).save(*args, **kwargs)

非常感谢您的解释。

0 个答案:

没有答案