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.
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
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'):
...