python - How to use StringIO with imghdr to determine if valid image
Asked Answered
A

1

6

How to use StringIO with imghdr to determine if valid image

I loaded a image into

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
Anubis answered 18/3, 2014 at 23:13 Comment(0)
C
10

imghdr.what takes the optional second argument. If that is 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())

with:

print imghdr.what(None, image_file.getvalue())
Chenay answered 18/3, 2014 at 23:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.