0

I want to check (image_file) variable format and verify if it's ".png" or ".jpg".

image_str = urlopen(url).read()
image_file = io.BytesIO(image_str)

url is a random file from reddit.

martineau
  • 119,623
  • 25
  • 170
  • 301
First785
  • 1
  • 1

2 Answers2

0

The best library for this is probably imghdr which inspects images and identifies their file-type:

import imghdr

imghdr.what("/path/to/file")
# OR...
imghdr.what("", h=io.BytesIO(imge_str))

The documentation for the library is here: https://docs.python.org/3/library/imghdr.html

match
  • 10,388
  • 3
  • 23
  • 41
0

You can use endswith

In your case if url is a string with your file, you can use :

if url.endswith('.png'):
    ...
elif url.endswith('.jpg'):
    ...