'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
    try:
        from PIL import Image
    except ImportError:
        import Image
    if hasattr(data, 'temporary_file_path'):
        file = data.temporary_file_path()
    elif hasattr(data, 'read'):
        file = StringIO(data.read())
    else:
        file = StringIO(data['content'])
    try:
        trial_image = Image.open(file)
        trial_image.load()
        if hasattr(file, 'reset'):
            file.reset()
        trial_image = Image.open(file)
        trial_image.verify()
    except ImportError:
        raise
    except Exception:
        raise ValidationError(self.error_messages['invalid_image'])
    if (hasattr(f, 'seek') and callable(f.seek)):
        f.seek(0)
    return f
