Example #1
0
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
Example #2
0
 def __init__(self,query="Rocket",results=50, with_cache=None,rescale=None):
   done=False
   self.rescale=rescale
   if (with_cache):
     try:
        os.stat(with_cache+"/"+query.lower())
        f=file(with_cache+"/"+query+"/reader.pcl","rb")
        self.query=pickle.load(f)
        self.urls=pickle.load(f)
        self.destdir=pickle.load(f)
        self.lq=pickle.load(f)
        done=True
     except: 
        pass
   if not done:
     from pycvf.core import settings
     from yahoo.search.image import ImageSearch
     yahoo_image_search=ImageSearch(settings.YAHOOAPPID)
     yahoo_image_search.query = query
     yahoo_image_search.results = results
     r=yahoo_image_search.get_results()
     self.query=query
     self.urls=[ ]
     for i in range(40):
        try:
           self.urls.append(r.childNodes[0].getElementsByTagName('Result')[i].getElementsByTagName('Url')[0].childNodes[0].nodeValue)
        except:
           pass
     if not (with_cache):
       self.destdir="/tmp/"+yahoo_image_search.query.lower()
     else:
       self.destdir=with_cache+yahoo_image_search.query.lower()
     try:
       os.mkdir(self.destdir)
     except:
       pass
     self.lq=len(self.urls)
     dl=map(lambda i:(self.urls[i],(self.destdir+"/f-"+str(i)+"."+self.urls[i].split(".")[-1]).lower()) ,range(len(self.urls)))
     if (self.lq):
       md=MultiDownloader(dl)
       md.run()
   if (with_cache):
     try:
       os.mkdir(with_cache+"/"+query.lower())
     except:
       pass
     f=file(with_cache+"/"+query+"/reader.pcl","wb")
     pickle.dump(self.query,f,protocol=2)
     pickle.dump(self.urls,f,protocol=2)
     pickle.dump(self.destdir,f,protocol=2)
     pickle.dump(self.lq,f,protocol=2)
     f.close()
Example #3
0
 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.")
Example #4
0
File: hw6.py Project: djames/ay250
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)
Example #5
0
    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()