[Django]-How does Django determine if an uploaded image is valid?

12👍

According to Django’s source code. Those three lines are responsible for verifying images:

from PIL import Image
trial_image = Image.open(file)
trial_image.verify()

The image type could be unsupported by PIL. Check the list of supported formats here

2👍

Did you try uploading image format like gif or png? It might be that your PIL was not built with the jpeg lib properly. I had a similar issue with Django on Ubuntu. If you have ever seen the error message decoder jpeg not available, check this link. Relevant line from the link:

$ cd libImaging
$ ./configure --with-jpeg=/somelib/lib --with-zlib=/somelib/lib

0👍

I looked into the Django source, in django/forms/fields.py, in the ImageField class. Django actually does use PIL to determine when an image is valid.

Leave a comment