Django图像文件的内容类型

时间:2013-04-27 13:25:07

标签: python django image identification imghdr

我希望在上传之前检查文件类型:

content = self.cleaned_data['picture']
content_type = content.content_type.split('/')[0]

当我上传图片时出现错误:

'NoneType' object has no attribute 'content_type'

这里有什么不妥?

1 个答案:

答案 0 :(得分:4)

如果图像有效,

imghdr.what将返回图像格式为字符串(gif,png等),否则返回None

用法:

import imghdr
imghdr.what(value)

在上面的示例中,value可以是文件(类似)对象或文件名。