How do I resize an image using PIL and maintain its aspect ratio?

后端 未结 20 1889
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:30

Is there an obvious way to do this that I\'m missing? I\'m just trying to make thumbnails.

20条回答
  •  星月不相逢
    2020-11-22 02:51

    I resizeed the image in such a way and it's working very well

    from io import BytesIO
    from django.core.files.uploadedfile import InMemoryUploadedFile
    import os, sys
    from PIL import Image
    
    
    def imageResize(image):
        outputIoStream = BytesIO()
        imageTemproaryResized = imageTemproary.resize( (1920,1080), Image.ANTIALIAS) 
        imageTemproaryResized.save(outputIoStream , format='PNG', quality='10') 
        outputIoStream.seek(0)
        uploadedImage = InMemoryUploadedFile(outputIoStream,'ImageField', "%s.jpg" % image.name.split('.')[0], 'image/jpeg', sys.getsizeof(outputIoStream), None)
    
        ## For upload local folder
        fs = FileSystemStorage()
        filename = fs.save(uploadedImage.name, uploadedImage)
    

提交回复
热议问题