def get_url_of_image(query_string): '''gets an image from a yahoo image search of "query string", returns a url''' srch = ImageSearch(app_id="YahooDemo", query='%s'%query_string) srch.results = 1 for each in srch.parse_results(): url = str(each.Url) return url
def _run_query_fired(self): """ Queries Yahoo! image search for with the query string """ # This is where we run the actual query once the user tells the GUI to # do so. We're using the pYsearch module to do a Yahoo image search, # which basically exposes a list of image urls. srch = ImageSearch(app_id="YahooDemo", query=self.query_string) # Run the search and return the top 10 results srch.results = 10 # We make a list of the top 10 image urls. imagrul_list = [] for res in srch.parse_results(): imagrul_list.append(res.Url) # We will try each image url in succession to download the image. We # need to do this in a complicated control loop because not all image # urls returned from the search still work. We need to try/except # the urllib2 connection errors and check that the image url linked to # a valid image we can download. If not, move on to the next url. If # no image urls were returned in the search (for example, if the search # was too specific and returned no results), print an explanation # in the GUI by writing it into the image_url string. final_imgurl = False for imgurl in imagrul_list: print "Connecting to", imgurl error_encountered = False try: response = urllib2.urlopen(imgurl) except: error_encountered = True print "Oh no, HTTP error encountered on " + imgurl if not error_encountered: final_imgurl = imgurl break if final_imgurl: print "Successfully accessed", final_imgurl self.image_url = final_imgurl image_string = response.read() response.close() image_file = file("image.jpg", "w") image_file.write(image_string) image_file.close() image_file = file("image_original.jpg", "w") image_file.write(image_string) image_file.close() self.display_image() print "Retrieved image and saved it as image.jpg" else: print ("Failed to retrieve any image, no " + "results found.") self.image_url = ("Failed to retrieve any image, " + "no results found.")
def getImage(q) : srch = ImageSearch(app_id="YahooDemo",query=q) # get one result in png format srch.results = 1 srch.format = "png" for res in srch.parse_results(): # the file-like object returned by # urllib doesn't implement some of the methods needed by imread. i # tried to read() the urllib object and convert that to a string # stream, but imread didn't like that, either. i knew creating # a file and reading it would work, so i did it. commands.getoutput("wget '"+res.Url+"' -O tmpfile") x = plt.imread("tmpfile") commands.getoutput("rm tmpfile") # images get reversed for some reason, reverse it back x = x[::-1] return (x, res.Url)
def getimage(self,query): """ Uses the yahoo module to run a Yahoo image search. It reads the linked image as a hex string. Then it saves it into a temporary file and reads it back using Image.open. It then displays the image. """ srch = ImageSearch(app_id="YahooDemo", query=self.query,results=1) for a in srch.parse_results(): url = a.Url imstring = urllib2.urlopen(url).read() output = open('temp.jpg','wb') output.write(imstring) output.close() self.url = url self.im = Image.open('temp.jpg') self.im = self.im.rotate(180) self.display()