Open a picture via URL and detecting if it is png, jpg or gif in Python?

2.8k Views Asked by At

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.

4

There are 4 best solutions below

1
On

You can use the imghdr library (included in the Python Standard Library) to determine the type of an image.

import cStringIO
import imghdr
import urllib2

url = "http://www.gnu.org/graphics/gerwinski-gnu-head.png"
response = urllib2.urlopen(url)
data = cStringIO.StringIO(response.read())
print(imghdr.what(data))
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

List of image mimes you can easily check against

0
On
 import os

def get_extension(image_url):
    _, extension = os.path.splitext(image_url)
    return extension

image_url = "https://example.com/images/my_image.jpg"
print(get_extension(image_url)) # Output: .jpg