Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How to compress image field in django?

'''
Grab all the code below

Then, you just need to create a model like this:
class MyModel(models.Model):
	profile_img = CompressedImageField()
'''

from io import BytesIO
from django.core.files import File
from django.db.models.fields.files import ImageField, ImageFieldFile
from PIL import Image

class CompressedImageFieldFile(ImageFieldFile):
    
    def save(self, name: str, content: File, save: bool = ...) -> None:
        if not content.readable():
            return

        # open the file as PIL image
        image = Image.open(content)
        # set up an in-memory byte io interface
        inmem_io = BytesIO()

        # get width and format information
        img_width, _ = image.size
        img_format = image.format
        
        # checking the width
        if img_width > self.field.thumbnail_width:
            # create the thumbnail
            image.thumbnail((
                int(self.field.thumbnail_width),
                int(self.field.thumbnail_width)
            ), Image.LANCZOS)

        # save the results to the in-memory file
        image.save(inmem_io, img_format)
        # change content to the new file
        content = File(inmem_io, name=content.name)

        return super().save(name, content, save=save)

class CompressedImageField(ImageField):

    attr_class = CompressedImageFieldFile

    def __init__(self, *args, thumbnail_width=500, **kwargs) -> None:
        self.thumbnail_width = thumbnail_width
        super().__init__(*args, **kwargs)

    def deconstruct(self):
        _name, _path, _args, kwargs = super().deconstruct()
        self._set_extra_kwargs(kwargs)
        return (_name, _path, _args, kwargs)

    def _set_extra_kwargs(self, kwargs):
        if self.thumbnail_width != 500:
            kwargs['thumbnail_width'] = self.thumbnail_width
Comment

PREVIOUS NEXT
Code Example
Python :: matplotlib radial averaging 
Python :: regex emaple py 
Python :: credential not provided when i try to sign up a new user django 
Python :: sample one point from distribution python 
Python :: Brainf**k Interpreter in Python 
Python :: django auto complete light styling 
Python :: fast guess for divisible numbers between two numbers 
Python :: Kinesis Client get_records response json 
Python :: convert timestamp datetime to int no astype 
Python :: machine learning cheatsheet activation function 
Python :: tar: Exiting with failure status due to previous errors 
Python :: python .exe long start 
Python :: django rest framework not getting form 
Python :: django route accept params with character 
Python :: python global variable that can be iterated 
Python :: pandas drop unnamed columns grebber 
Python :: simplejwt in django setup 
Python :: torch.unsqueze 
Python :: Sorted iteration 
Python :: Cget subassembly civid3d 
Python :: python types generator 
Python :: How to multiply a text in python 
Python :: hexing floats 
Python :: take input from clipboard python 
Python :: count wit for loop pthoon 
Python :: shotgun meanign 
Python :: convert from python code to c++ code 
Python :: how to convert ui file to py file 
Python :: recursionerror maximum recursion depth exceeded in comparison 
Python :: how to get user input in python 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =