How to add image size validation to Django backend?

If you want a user to upload an image that is at least 500x500px in size (this is an example size), you can add validator to your model:

def minimum_size(width=None, height=None):

    def validator(image):
        if not image.is_image():
            raise ValidationError('File should be image.')

        (errors, image_info) = ([], image.info()['image_info'])
        if width is not None and image_info['width'] < width:
            errors.append('Width should be > {} px.'.format(width))
        if height is not None and image_info['height'] < height:
            errors.append('Height should be > {} px.'.format(height))
        raise ValidationError(errors)

    return validator

Code snippet.

Also, you can perform validation on front-end, even before an image gets uploaded: Content Moderation