Django image resizing and convert before upload

后端 未结 8 1194
余生分开走
余生分开走 2020-12-07 19:15

I searched a lot on this subject but couldn\'t really find what I need. I\'ll explain my problem :

On my website, the user can upload an image. I need to resize this

相关标签:
8条回答
  • 2020-12-07 20:13

    EDIT4 : full working code can be found here (the code below still has some mistakes)

    Still fighting, but there is progress ^^ : I am trying to do the resizing and convertion in memory with PIL (and seeing the amount of questions on the subject, it seems that I am not the only one ^^). My code so far (in Models.py):

    from PIL import Image as Img
    import StringIO
    from django.core.files import File
    
    class Mymodel(models.Model):
    #blablabla
    photo = models.ImageField(uppload_to="...", blank=True)
    
        def save(self, *args, **kwargs):
            if self.photo:
                image = Img.open(StringIO.StringIO(self.photo.read()))
                image.thumbnail((100,100), Img.ANTIALIAS)
                output = StringIO.StringIO()
                image.save(output, format='JPEG', quality=75)
                output.seek(0)
                self.photo = File(output, self.photo.name())
            super(Mymodel, self).save(*args, **kwargs)
    

    I have an error at "photo = File(output, photo.name())"

    Thanks

    EDIT : Sorry, it was a simple mistake (forgot the self.) corrected in code. Now I have the error "TypeError, 'unicode' object is not callable", at the same line.

    EDIT2: Saw something about "output.getvalue()" here but don't really know if could be of any help.

    EDIT3 : Solved!! Code below.

    from PIL import Image as Img
    import StringIO
    from django.core.files.uploadedfile import InMemoryUploadedFile
    
    class Mymodel(models.Model):
        photo = models.ImageField(upload_to="...", blank=True)
    
        def save(self, *args, **kwargs):
            if self.photo:
                image = Img.open(StringIO.StringIO(self.photo.read()))
                image.thumbnail((200,200), Img.ANTIALIAS)
                output = StringIO.StringIO()
                image.save(output, format='JPEG', quality=75)
                output.seek(0)
                self.photo= InMemoryUploadedFile(output,'ImageField', "%s.jpg" %self.photo.name, 'image/jpeg', output.len, None)
            super(Mymodel, self).save(*args, **kwargs)
    

    For PNG looking weird, see second post on this thread

    0 讨论(0)
  • 2020-12-07 20:16

    Here is an app that can take care of that: django-smartfields. It will also remove an old image whenever a new one is uploaded.

    from django.db import models
    
    from smartfields import fields
    from smartfields.dependencies import FileDependency
    from smartfields.processors import ImageProcessor
    
    class ImageModel(models.Model):
        image = fields.ImageField(dependencies=[
            FileDependency(processor=ImageProcessor(
                format='JPEG', scale={'max_width': 300, 'max_height': 300}))
        ])
    
    0 讨论(0)
提交回复
热议问题