def clean_upload_data(data):
    image = data['image']
    image.seek(0)
    try:
        pil_image = PIL.Image.open(image)
    except __HOLE__ as e:
        if e.errno:
            error_msg = force_unicode(e)
        else:
            error_msg = u"Invalid or unsupported image file"
        raise forms.ValidationError({"image": [error_msg]})
    else:
        extension = get_image_extension(pil_image)

    upload_to = data['upload_to'] or None
    folder_path = get_upload_foldername(image.name, upload_to=upload_to)

    (w, h) = (orig_w, orig_h) = pil_image.size
    sizes = data.get('sizes')
    if sizes:
        (min_w, min_h) = get_min_size(sizes)

        if (orig_w < min_w or orig_h < min_h):
            raise forms.ValidationError({"image": [(
                u"Image must be at least %(min_w)sx%(min_h)s "
                u"(%(min_w)s pixels wide and %(min_h)s pixels high). "
                u"The image you uploaded was %(orig_w)sx%(orig_h)s pixels.") % {
                    "min_w": min_w,
                    "min_h": min_h,
                    "orig_w": orig_w,
                    "orig_h": orig_h
                }]})

    if w <= 0:
        raise forms.ValidationError({"image": [u"Invalid image: width is %d" % w]})
    elif h <= 0:
        raise forms.ValidationError({"image": [u"Invalid image: height is %d" % h]})

    # File is good, get rid of the tmp file
    orig_file_path = os.path.join(folder_path, 'original' + extension)
    image.seek(0)
    image_contents = image.read()
    with open(os.path.join(settings.MEDIA_ROOT, orig_file_path), 'wb+') as f:
        f.write(image_contents)
    md5_hash = hashlib.md5()
    md5_hash.update(image_contents)
    data['md5'] = md5_hash.hexdigest()
    data['image'] = open(os.path.join(settings.MEDIA_ROOT, orig_file_path), mode='rb')
    return data
dataset/ETHPy150Open theatlantic/django-cropduster/cropduster/views/forms.py/clean_upload_data