2

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.

Hanxue
  • 12,243
  • 18
  • 88
  • 130
user3760874
  • 75
  • 1
  • 5

3 Answers3

3

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

CasualDemon
  • 5,790
  • 2
  • 21
  • 39
2

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))
Christian Berendt
  • 3,416
  • 2
  • 13
  • 22
1

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')
Victor
  • 372
  • 3
  • 9
dusty
  • 865
  • 9
  • 15