def get_heroes_images(self, size="lg", force=False, ratio=1.0): """ Get all the hero images and store them in data/images """ loc = "data/images_{}".format(size) if not os.path.exists(loc): os.makedirs(loc) os.chdir(loc) log.basicConfig(level=log.INFO) log.warning("Downloading all hero images. This will take a few minutes.") for hero in api.get_heroes()["result"]["heroes"]: # if the hero is Abyssal Underlord, ignore because he's not in the game yet if hero["name"].find("abyssal_underlord") != -1: continue if not os.path.exists("{}.png".format(hero["localized_name"])) or force: if ratio == 1.0: with open("{}.png".format(hero["localized_name"]), "wb") as outfile: log.info("Getting hero image from {}".format(api.get_hero_image_url(hero["name"][len("npc_dota_hero_"):]))) outfile.write(urllib2.urlopen(api.get_hero_image_url(hero["name"][len("npc_dota_hero_"):], image_size=size)).read()) outfile.close() else: log.info("Getting hero image from {}".format(api.get_hero_image_url(hero["name"][len("npc_dota_hero_"):]))) img = Image.open(cStringIO.StringIO(urllib2.urlopen(api.get_hero_image_url(hero["name"][len("npc_dota_hero_"):])).read())) img = img.resize((int(89 * ratio), int(50 * ratio)), Image.ANTIALIAS) img.save("{}.png".format(hero["localized_name"]))
import os, urllib2, cStringIO from dota2py import api import Image if ___name__ == "__main__": if not os.path.exists("../data/images"): os.makedirs("../data/images") os.chdir("../data/images") scale = 50/1080.0 for hero in api.get_heroes()["result"]["heroes"]: if "abyssal_underlord" in hero["name"]: continue #if not os.path.exists("{}.png".format(hero["localized_name"])): print api.get_hero_image_url(hero["name"][len("npc_dota_hero_"):], "lg") img = Image.open(cStringIO.StringIO(urllib2.urlopen(api.get_hero_image_url(hero["name"][len("npc_dota_hero_"):])).read())) img = img.resize((89, 50), Image.ANTIALIAS) img.save("{}.png".format(hero["localized_name"]))
def test_get_hero_image(self): """ Get a hero image """ image_url = api.get_hero_image_url('lion', 'full') self.assertEquals(HERO_IMAGE, image_url)
def match_hero_image(name): return api.get_hero_image_url(name)