Package: ecosia-images
python-ecosia-imagesPython module for searching and downloading images from EcosiaInstallingpip install ecosia-imagesSetupThe only requisite to work with the library is to have a web browser and its driver installed. Currently the package works with either Google Chrome or Firefox.If using Google Chrome it is required to also have Chromedriver installed and reachable in PATH. See thefollowing linkfor more information.As for Firefox, Geckodriver is required to be installed and reachable in PATH.ExamplesStart a crawler>>>fromecosia_imagesimportcrawler>>>searcher=crawler()The browser to be used can be passed to the crawler constructor>>>fromecosia_imagesimportcrawler>>>searcher=crawler(browser='firefox')To see all valid browser options, seeecosia_images.browser_options.>>>fromecosia_imagesimportbrowser_options>>>browser_options['chrome','firefox']Search images and get the links to the picturesAfter declaring a crawler and using it to search for a keyword, the resulting links will be accesible by thelinksproperty>>>searcher=crawler()>>>searcher.search('number 9')>>>searcher.links{...}# urlsSearch with optionsSearches can also include the options that Ecosia provides for refining results. The available keys and values for refining searches are stored inecosia_images.download_options.>>>fromecosia_imagesimportdownload_options>>>download_options{'size':['small','medium','large','wallpaper'],'color':['colorOnly','monochrome','red','orange','yellow','green','teal','blue','purple','pink','brown','black','gray'],'image_type':['photo','clipart','line','animated'],'freshness':['day','week','month'],'license':['share','shareCommercially','modify','modifyCommercially','public']}The selected options can be specified when calling thesearchmethod of the crawler.>>>searcher.search('trees',color='monochrome',size='wallpaper')>>>searcher.links{...}# links to big pictures of trees in black and whiteGather more linksIf more links are needed the functiongather_morecan be used.>>>searcher.search('bees')>>>len(searcher.links)50# Give or take>>>searcher.gather_more()>>>len(searcher.links)100# Give or takeDownload imagesIn all the following cases, the script first checks whether the image has been already downloaded so it does not download it again. The functions will return the file paths to the downloaded images.Thedownloadfunction will download a given number of pictures and save them in a folder whose name coincides with the keyword. This folder will be created inside the one specified when calling the constructor. In the following example, the images would be saved inside/path/to/folder/keyword/.>>>searcher=crawler(directory='/path/to/folder/')>>>searcher.search('keyword')>>>searcher.download(100)[...]# list with file pathsIf no folder is specified, the images will be saved inside a new folder nameddownloadslocated in the current working directory.There is also thedownload_allfunction which will download all the currently available links in the crawler object>>>searcher.search('pidgeons')>>>searcher.download_all()[...]Stoping the clientIt is necessary to stop the crawler to avoid the leak of resources.>>>searcher.stop()FilenamesThe naming convention to be used for the downloaded files can be passed to the crawler constructor. To see all valid naming options, seeecosia_images.naming_options.>>>fromecosia_imagesimportcrawler,naming_options>>>searcher=crawler(naming='hash')>>>naming_options['trim','hash']Custom namingFor a specific naming convention, a function can be passed to the constructor. If you are planning to rename the files, make sure to use this functionality as renaming the files will interfere with the crawler's ability to avoid downloading duplicates.The function must take three parameters: url, directory and keyword.If you plan on not using the default folders provided by the library, disallow this option so no directories are created by the crawler.>>>defcustom_naming(url,directory,keyword):...# Function implementation...returnfilename>>>searcher=crawler(naming_function=custom_naming,makedirs=False)DisclaimerThe dowloaded images come from the Ecosia search engine and they may have copyrights. Do not download or use any image that violates its copyright terms. You can take advantage of thelicenseoption of thesearchfunction to avoid using copyrighted material.