Python - How to use StringIO with imghdr to determine image validity

How to use StringIO with imghdr to determine the correct image

I uploaded the image to

image_file = StringIO(open("test.gif",'rb').read())
imghdr.what(image_file.getvalue())

    print imghdr.what(image_file.getvalue())
  File "/usr/lib/python2.7/imghdr.py", line 12, in what
    f = open(file, 'rb')
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
+4
source share
1 answer

imghdr.whataccepts an optional second argument. If specified, the first argument is ignored and the second argument is assumed to contain image bytes.

So you can change the following line:

print imghdr.what(image_file.getvalue())

from:

print imghdr.what(None, image_file.getvalue())
+6
source

Source: https://habr.com/ru/post/1532385/


All Articles