2

I want to determine if a buffer I have (downloaded it) is an image file, without saving it to the disk. I looked it up and found out that:

  1. imghdr can do it only for files.
  2. python-magic can give me the file type, but than I need to map it to image types.

Is there a better way?

Hanxue
  • 12,243
  • 18
  • 88
  • 130
yeger
  • 351
  • 2
  • 15

1 Answers1

7

As per the documentation, you can identify the file type with imghdr via a byte stream (which you should be able to get from your buffer)

import imghdr
imghdr.what('', byte_stream)

If you supply the second parameter the filename will be ignored.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
KRONWALLED
  • 1,422
  • 11
  • 12