| 176 | | Image.open(StringIO(content)) |
| 177 | | except IOError: # Python Imaging Library doesn't recognize it as an image |
| 178 | | raise ValidationError, gettext("Upload a valid image. The file you uploaded was either not an image or a corrupted image.") |
| | 178 | # load() is the only method that can spot a truncated JPEG, |
| | 179 | # but it cannot be called sanely after verify() |
| | 180 | trial_image = Image.open(StringIO(content)) |
| | 181 | trial_image.load() |
| | 182 | # verify() is the only method that can spot a corrupt PNG, |
| | 183 | # but it must be called immediately after the constructor |
| | 184 | trial_image = Image.open(StringIO(content)) |
| | 185 | trial_image.verify() |
| | 186 | except Exception, error: # Too many exception types from PIL to enumerate properly |
| | 187 | message = invalid_file_msg |
| | 188 | if settings.DEBUG: |
| | 189 | message = message + " Error reported by the Python Imaging Library: " + str(error) |
| | 190 | raise ValidationError(message) |