Skip to 0 minutes and 3 seconds Now it’s time for you to use the pokebase, requests and PIL external modules to download and display images of Pokémon. First, let’s create a new Python programme and save it as pokemonsimple.py. In the previous step, you should have installed the pokebase module, which you can import the top of your programme. I’m going to say from pokebase import the Pokémon function. You’re also going to use the webbrowser function, which comes installed with Python. So we’re going to import that too.
Skip to 0 minutes and 39 seconds Now I’m going to define a new function called fetch_pokemon which will prompt the user to enter the name of a Pokémon.
Skip to 0 minutes and 47 seconds Once I have the name of a Pokémon, I can use the Pokémon function we just imported to download information about that Pokémon.
Skip to 0 minutes and 59 seconds Now the poke variable is going to hold all the attributes about your downloaded Pokémon, such as its weight, its height, or its abilities. And you can use print to output some of this information. So for example, I’m going to output my Pokémon’s weight. So I’m going to say name, weighs, and we’ll say poke.weight.
Skip to 1 minute and 22 seconds Finally, I need to call my function, which I do so using its name and then opening a bracket and closing a bracket. So let’s give our program a run and see what happens. So we run our program. It prompts us, which Pokémon do you wish to fetch? I’m going to fetch Charizard.
Skip to 1 minute and 40 seconds And I get an error. Because the weight of a Pokémon is an integer. And I need to cast it to a string. So let’s just try that again. Charizard. Fetching Charizard. And now it tells me Charizard weighs 905, presumably, grammes. Now by using the DIR function, you can output a list of all of the attributes within the Poke variable. So let’s try that and see what we can see. So we going to say print. And we’re going to use DIR. And we’re going to use the Poke variable, because that’s what we want to see inside of. Let’s see what that gives us. So we’re going to run our program again. Which Pokémon to you wish to fetch?
Skip to 2 minutes and 20 seconds I wish to fetch Charizard. And it gives me a list, a list of all of the attributes that are within the Poke variable. And we can see there is the weight one that we just used. There’s also one called Sprites. So let’s have a look what’s inside that. So to do that, I’m going to print out what is inside that. So I’m going to say Poke.Sprites. Run my program again.
Skip to 2 minutes and 44 seconds It fetches Charizard and shows me– prints out to the screen, the content of that Sprites attribute. And I can see it contains a number of links to images. Now by using the web browser module, we can automatically open up one of those images. So let’s do that. So let’s say, webbrowser.open. And I’m going to say open Poke.sprites. And there was a sprite called front_default And so let’s run that and see what happens. Again, it’s going to ask me for a Pokémon name. I’m going to put Charizard. Fetches Charizard, opens up my web browser, and shows me an image of that Pokémon.
Skip to 3 minutes and 26 seconds Now can you follow the instructions in the article below to download and save the Pokémon’s image using the requests and the PIL module? How do you think activities like this one could be used to encourage learners to use text based programming languages? Share your answers in the comments below.
You are going to use the
PIL external modules to download and display images of Pokémon.
Create a new Python program and save it as
A function is a named block of code that performs a task. Functions are useful for keeping code organised, and because you can reuse them.
Add the following code to create a
fetch_pokemonfunction which will prompt the user to enter the name of a pokemon.
def fetch_pokemon(): name = input('Which Pokemon do you want to fetch? ') print("Fetching " + name)
Call (run) the
fetch_pokemonfunction by using its name, followed by a pair of round brackets.
def fetch_pokemon(): name = input('Which Pokemon do you want to fetch? ') print("Fetching " + name) fetch_pokemon()
Run your program, you will be prompted to enter the name of a Pokémon.
Use pokebase to get a Pokémon
Now for the fun part! You are going to use
pokebase to get some information about the Pokémon.
pokebase is a library that provides access to PokéAPI, a Pokémon API. API stands for application programming interface, which is a set of tools for building software applications — in this case, apps to do with Pokémon.
You can have a look at the website pokeapi.co to find out more about this particular API, and try out its online version. If you want to find out about the Pokémon Charizard for instance, you could search for
pokebase module gives us tools to write a program that does the same thing: to look up different Pokémon and get information about them.
You are also going to use the standard
webbrowser module to display an image of the Pokémon you downloaded.
webbrowsermodules into your program, at the top of your program:
from pokebase import pokemon import webbrowser
Note: You have used two different methods for importing modules.
from pokebase import pokemonimports a single function,
pokemon, from the
pokemonfunction can be called directly by typing
import webbrowserimports the whole
webbrowsermodule. Functions within the
webbrowsermodules will have to be called first using the name of the module, for example
The choice of which method to use depends on how your intend to use the module and structure your program. There is no right or wrong method.
fetch_pokemonfunction to use the
pokemonfunction to download your Pokémon.
def fetch_pokemon(): name = input('Which Pokemon do you want to fetch? ') print("Fetching " + name) poke = pokemon(name)
pokevariable holds all the attributes about your downloaded Pokémon, such as its
... poke = pokemon(name) print(name + " weighs " + str(poke.weight))
Run your program and enter the name of a Pokémon. If you don’t know any, type in
You can use the code
print(dir(poke))to output a list of all the attributes in the
pokevariable. What other information can you find out about your Pokémon?
fetch_pokemonfunction to print out the
... poke = pokemon(name) print(poke.sprites)
Run the code and enter
charizardwhen asked for a Pokémon. The data that is printed shows the links to different images of Charizard that the API gives you access to. To check out the images, you can copy and paste the links into a browser.
You can use the
webbrowsermodule to automatically open one of these images in your web browser.
Add the following code to the bottom of your
fetch_pokemonfunction to open the
... poke = pokemon(name) webbrowser.open(poke.sprites.front_default)
Save the image using requests and PIL
To save the image on your computer you are going to use:
getthe data from the URL
PILmodule to save the data as a
Add this code to import the modules at the top of your program.
from requests import get from PIL import Image from io import BytesIO
fetch_pokemonfunction to download the image data using the
def fetch_pokemon(): ... poke = pokemon(name) pic = get(poke.sprites.front_default).content
At the moment the
picis just a bunch of
0s on your computer, so it needs to be converted into something humans can look at using the
Add this code to create an
Imagefrom the image data and save it as a gif file:
def fetch_pokemon(): ... pic = get(poke.sprites.front_default).content image = Image.open(BytesIO(pic)) image.save('poke.gif')
Once you type in the name of a Pokémon, your program will fetch the
front_default image from the website and save it in the directory where you saved your Python file. Run the program a few times and enter different Pokémon, for example
pikachu. After each run open the saved
poke.gif file — the image file gets overwritten each time.
How do you think activities like this one could be used to encourage learners to use text-based programming languages? Share your answers in the comments.