14

I have an ImageField in one of my models so that users can upload an image. When a user submits an upload form, I want to verify that the file in question is a fully valid and displayable image.

I tried using PIL to verify that the image was in fact authentic, but using

from PIL import Image Image.open(model.file) Image.verify() 

No matter what file I give it though, it always throws an exception.

Anyone know of an easy way to verify the file?

4 Answers 4

10

Good news, you don't need to do this:

class ImageField(upload_to=None, height_field=None, width_field=None, max_length=100, **options)

Inherits all attributes and methods from FileField, but also validates that the uploaded object is a valid image.

https://docs.djangoproject.com/en/1.10/ref/models/fields/#django.db.models.ImageField

Sign up to request clarification or add additional context in comments.

5 Comments

This is correct, but it appears it doesn't do this all by itself. It requires that you setup the form in forms.py in order for the validation to occur.
Were you handling the upload by processing the raw post data?
Yes. I was trying to handle it all in the views method that I had setup. I set it up with a forms system as well as the is_valid() method and that worked like a charm.
Glad it works. Django forms is definitely the way to go
@Zach Sugano can you provide source that ImageField validation only works with Django forms? Would be very helpful. I'd need image validation, but Django is only a backend, so can't use their forms.
6

Also, you should use verify() as follows:

from PIL import Image im = Image.open(model.file) im.verify() 

2 Comments

I thinks pillow will raise exception when open image instead of need to verify after image opened. So, secondth line will raise IOError if image is not valid.
This is what Django's ImageField does already. See the source: github.com/django/django/blob/…
2

you can use a 'Pillow' with 'try,except 'block, before insert image/data to a database or use it where you want,

like my following example for submit ticket support form , 'view.py' file :

from PIL import Image if request.method=='POST': # check if attachment file is not empty inside try/except to pass django error. try: ticket_attachmet_image = request.FILES["q_attachment_image"] except: ticket_attachmet_image = None # check if uploaded image is valid (for example not video file ) . if not ticket_attachmet_image == None: try: Image.open(ticket_attachmet_image) except: messages.warning(request, 'sorry, your image is invalid') return redirect('your_url_name') 

#done.

1 Comment

here you have posted code from a function without showing the function. People using a class based view will not understand how this could possibly be implemented
0
if 'image' in request.FILES['image'].content_type: # Some code else: # the file is not image 

The ImageField doesn't work when the form is not created by the form.py

In fact, if we upload a file, that is not an image, and save it in the image field, it wouldn't raise any error so, the content_type of the file must be checked before saving.

4 Comments

content_type can be manipulated; you're effectively trusting users to send the correct content type which is insecure. Files must be independently verified they are the correct file type. From the Django docs: docs.djangoproject.com/en/4.0/ref/files/uploads/…
is this "request.FILES" exclusively for a function-based view? the documentation loves to omit details like this
FILES will only contain data if the request method was POST and the <form> that posted to the request had enctype="multipart/form-data", So in class-based views it contains data in POST method or, in function-based views if the request.method is 'POST'. @Conor
thanks yes I figured out about those CreateView functions just yesterday (new to Django)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.