如何在保存之前用PIL调整上传文件的大小?

时间:2014-04-19 10:08:59

标签: python django python-imaging-library

我意识到这是一项微不足道的任务,这个问题已被多次回答,但我无法得到它。有没有办法在将图像保存到磁盘之前调整大小并裁剪图像?我发现的所有解决方案都倾向于存储图像,调整大小,然后再次存储。我可以这样做吗?

# extending form's save() method
def save(self):
    import Image as pil

    # if avatar is uploaded, we need to scale it
    if self.files['avatar']:
        img = pil.open(self.files['avatar'])
        img.thumbnail((150, 150), pil.ANTIALIAS)

        # ???
        # self.files['avatar'] is InMemoryUpladedFile
        # how do I replace self.files['avatar'] with my new scaled image here?
        # ???

    super(UserForm, self).save()

2 个答案:

答案 0 :(得分:6)

我能够弄清楚这一点。您只需将修改后的文件保存为StringIO,然后从中创建新的InMemoryUploadedFile即可。这是完整的解决方案:

def save(self):

    import Image as pil
    import StringIO, time, os.path
    from django.core.files.uploadedfile import InMemoryUploadedFile

    # if avatar is uploaded, we need to scale it
    if self.files['avatar']:
        # opening file as PIL instance
        img = pil.open(self.files['avatar'])

        # modifying it
        img.thumbnail((150, 150), pil.ANTIALIAS)

        # saving it to memory
        thumb_io = StringIO.StringIO()
        img.save(thumb_io,  self.files['avatar'].content_type.split('/')[-1].upper())

        # generating name for the new file
        new_file_name = str(self.instance.id) +'_avatar_' +\
                        str(int(time.time())) + \
                        os.path.splitext(self.instance.avatar.name)[1]

        # creating new InMemoryUploadedFile() based on the modified file
        file = InMemoryUploadedFile(thumb_io,
                                    u"avatar", # important to specify field name here
                                    new_file_name,
                                    self.files['avatar'].content_type,
                                    thumb_io.len,
                                    None)

        # and finally, replacing original InMemoryUploadedFile() with modified one
        self.instance.avatar = file

    # now, when form will be saved, it will use the modified file, instead of the original
    super(UserForm, self).save()

答案 1 :(得分:0)

我不熟悉PIL,但正如我从文档中看到的那样,您可以将文件对象作为“文件”参数传递给“打开”函数。

Django request.FILES存储UploadedFile对象 - 围绕上传文件的简单包装(存储在内存或临时文件中),它支持read,seek和tell操作,因此可以直接传递给PIL“open”功能