Reputation: 1567
I am making a website. I want to check from the server whether the link that the user submitted is actually an image that exists.
Upvotes: 10
Views: 23886
Reputation: 521
This is the best approach working for my application, based also on previous comments:
import requests
def is_url_image(image_url):
image_formats = ("image/png", "image/jpeg", "image/jpg")
r = requests.head(image_url)
if r.headers["content-type"] in image_formats:
return True
return False
Upvotes: 20
Reputation: 15209
This is one way that is quick:
It doesn't really verify that is really an image file, it just guesses based on file extention and then checks that the url exists. If you really need to verify that the data returned from the url is actually an image (for security reasons) then this solution would not work.
import mimetypes, urllib2
def is_url_image(url):
mimetype,encoding = mimetypes.guess_type(url)
return (mimetype and mimetype.startswith('image'))
def check_url(url):
"""Returns True if the url returns a response code between 200-300,
otherwise return False.
"""
try:
headers = {
"Range": "bytes=0-10",
"User-Agent": "MyTestAgent",
"Accept": "*/*"
}
req = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(req)
return response.code in range(200, 209)
except Exception:
return False
def is_image_and_ready(url):
return is_url_image(url) and check_url(url)
Upvotes: 14
Reputation: 509
You can read the header of the http request, it contains some meta-data like the content-type.
On python 3:
from urllib.request import urlopen
image_formats = ("image/png", "image/jpeg", "image/gif")
url = "http://localhost/img.png"
site = urlopen(url)
meta = site.info() # get header of the http request
if meta["content-type"] in image_formats: # check if the content-type is a image
print("it is an image")
You can also get other info like the size of the image and etc. The good news about this is that it doesn't download the image. It could fail if the header says that it is an image and it is not, but you can still do a last check and download the image if it pass the first filter.
Upvotes: 4
Reputation: 148
Take a look into imghdr
Here is some example code:
import imghdr
import httplib
import cStringIO
conn = httplib.HTTPConnection('www.ovguide.com', timeout=60)
path = '/img/global/ovg_logo.png'
conn.request('GET', path)
r1 = conn.getresponse()
image_file_obj = cStringIO.StringIO(r1.read())
what_type = imghdr.what(image_file_obj)
print what_type
This should return 'png'. If it is not an image it will return None
Hope that helps!
-Blake
Upvotes: 1