Get Image Dimensions from Url in Python -
i want image dimensions seen viewer on website.
i'm using beautiful soup , image links this:
links = soup.findall('img', {"src":true})
the way image dimensions using:
link.has_key('height') height = link['height']
and width well. however, links have 1 of these attributes. tried pil gives actual image size if downloaded.
is there other way of finding image dimensions seen on website?
your main issue searching html source references height , width. in cases (when things done well), images don't have height , width specified in html, in case rendered @ height , width of image file itself.
to height , width of image file, need query , load file, check height , width using image processing. if want, let me know, , i'll work through process.
import urllib, cstringio pil import image # given object called 'link' site_url = "http://www.targetsite.com" url = site_url + link['src'] # here's sample url works demo purposes # url = "http://therealtomrose.therealrosefamily.com/wp-content/uploads/2012/08/headshot_tight.png" file = cstringio.stringio(urllib.urlopen(url).read()) im=image.open(file) width, height = im.size if link.has_key('height'): height = link['height'] # set height if site modifies if link.has_key('width'): width = link['width'] # set width if site modifies
requirements: method requires pil library image processing.
# command line in virtual environment pip install pil
Comments
Post a Comment