I want to determine if a puush.me image (Links are no HTML, just the image) is png, jpg or gif. Is there a way to do that n python? Urllib does not seem to open images and detecting them.
Open a picture via URL and detecting if it is png, jpg or gif in Python?
2.8k views Asked by user3760874 At
4
There are 4 answers
1
On
If you've got the data of the file, you could check the first few bytes for magic number signiture:
- png 89 50 4E 47 0D 0A 1A 0A
- jpg FF D8 FF
- bmp 42 4D
- gif 47 49 46 38
For example, Python3.x:
with open('image', 'rb') as f:
bytes = f.read()
if bytes.startswith(b'89504E470D0A1A0A'):
print('PNG')
if bytes.startswith(b'FFD8FF'):
print('JPG')
if bytes.startswith(b'424D'):
print('BMP')
if bytes.startswith(b'47494638'):
print('GIF')
0
On
To determine what type of file it is from the webserver itself before downloading, you can check the Content-Type
header.
Python 2.x example
import urllib2
my_url = "http://upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png" #example image
request = urllib2.urlopen(my_url)
mime = request.info()['Content-type']
if mime.endswith("png"):
print("Image is a png")
elif mime.endswith("jpeg"):
print("Image is a jpg")
elif mime.endswith("gif"):
print("Image is a gif")
#Image is a png
You can use the imghdr library (included in the Python Standard Library) to determine the type of an image.