validate the size and format if uploaded image and resize it in django
<p>I am uploading an image in django, i want to validate it's format and size in forms.py</p>

<pre><code>class CreateEventStepFirstForm(forms.Form):
    user_image = forms.ImageField(required = True, widget=forms.FileInput(attrs={
        'class' : 'upload-img',
        'data-empty-message':'Please upload artist image, this field is required'
    }))
</code></pre>

<p>While uploading this image i want to first validate it's format, form allows user only to upload png and jpeg image and also user will have to upload an image upto 700*500 dimensions, if image is lower than this dimensions, then this form should not be validated, and if image is greater than 1200*1000 pixels, in this case it should resize image to 700*500 without affecting the image quality.</p>

<p>View i am using for uploading file is :-</p>

<pre><code>def create_new_event(request, steps):
    if request.method == 'POST':
        stepFirstForm = CreateEventStepFirstForm(request.POST, request.FILES)
        if stepFirstForm.is_valid():

            myfile = request.FILES['user_image']
            fs = FileSystemStorage()
            filename = fs.save('event_artists_images/'+myfile.name, myfile)
            uploaded_file_url = fs.url(filename)

        return render(request, 'home/create-new-event.html', {'stepFirstForm':stepFirstForm})
</code></pre>

<p>You should look at writing your own custom validator.  You can read about them here in the <a href="https://docs.djangoproject.com/en/1.10/ref/validators/" rel="nofollow">documentation</a> </p>

<p>Once you've created a validator that checks against those values then you can attach it to the form in a couple of different ways.</p>

python django