特定宽度和高度的Django表单Imagefield验证

root
abc abc
  • 20 Jul

Custom validator to validate the maximum size of images

def maximum_size(width=None, height=None):
from PIL import Image

def validator(image):
    img = Image.open(image)
    fw, fh = img.size
    if fw > width or fh > height:
        raise ValidationError(
        "Height or Width is larger than what is allowed")
 return validator

class Photo(models.Model):
image = models.ImageField('Image', upload_to=image_upload_path, validators=[maximum_size(128,128)])