'Checks that the file-upload field data contains a valid image (GIF, JPG,
PNG, possibly others -- whatever the Python Imaging Library supports).'

def to_python(self, data):

    f = super(ImageField, self).to_python(data)
    if (f is None):
        return None
    from django.utils.image import Image
    if hasattr(data, u'temporary_file_path'):
        file = data.temporary_file_path()
    elif hasattr(data, u'read'):
        file = BytesIO(data.read())
    else:
        file = BytesIO(data[u'content'])
    try:
        Image.open(file).verify()
    except Exception:
        six.reraise(ValidationError, ValidationError(self.error_messages[u'invalid_image'], code=u'invalid_image'), sys.exc_info()[2])
    if (hasattr(f, u'seek') and callable(f.seek)):
        f.seek(0)
    return f
