Using Python to Download and Display Pokémon Images
You are going to use the pokebase, requests, and PIL external modules to download and display images of Pokémon.
Which Pokémon?
-
-
Create a new Python program and save it as
pokemon_simple.py.
-
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)
-
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.
-
-
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()
-
Call (run) the
-
-
Run your program, you will be prompted to enter the name of a Pokémon.
-
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 pokemon/charizard.

The 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.
-
-
Import the
pokebaseandwebbrowsermodules into your program, at the top of your program:from pokebase import pokemon import webbrowserNote: You have used two different methods for importing modules.
from pokebase import pokemonimports a single function,pokemon, from thepokebasemodule. Thepokemonfunction can be called directly by typingpokemon().import webbrowserimports the wholewebbrowsermodule. Functions within thewebbrowsermodules will have to be called first using the name of the module, for examplewebbrowser.open()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.
-
Import the
-
-
Modify the
fetch_pokemonfunction to use thepokemonfunction to download your Pokémon.def fetch_pokemon(): name = input('Which Pokemon do you want to fetch? ') print("Fetching " + name) poke = pokemon(name)The
pokevariable holds all the attributes about your downloaded Pokémon, such as itsweight,height, orabilities.
-
Modify the
-
-
Use
printto output some information about your Pokémon:... poke = pokemon(name) print(name + " weighs " + str(poke.weight))
-
Use
-
-
Run your program and enter the name of a Pokémon. If you don’t know any, type in
charizard.
You can use the code
print(dir(poke))to output a list of all the attributes in thepokevariable. What other information can you find out about your Pokémon?
-
Run your program and enter the name of a Pokémon. If you don’t know any, type in
-
-
Modify the
fetch_pokemonfunction to print out thespritesattributes.... 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.
-
Modify the
-
-
Add the following code to the bottom of your
fetch_pokemonfunction to open thefront_defaultsprite URL.... poke = pokemon(name) webbrowser.open(poke.sprites.front_default)
-
Add the following code to the bottom of your
Save the image using requests and PIL
To save the image on your computer you are going to use:
-
- The
requestsmodule togetthe data from the URL
- The
-
Imagefrom thePILmodule to save the data as agif
-
-
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
-
Add this code to import the modules at the top of your program.
-
-
Modify your
fetch_pokemonfunction to download the image data using thegetfunction fromrequests.def fetch_pokemon(): ... poke = pokemon(name) pic = get(poke.sprites.front_default).contentAt the moment the
picis just a bunch of1s and0s on your computer, so it needs to be converted into something humans can look at using theioandPILmodules.
-
Modify your
-
-
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')
-
Add this code to create an
Download and Display Different Pokémon
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 mewtwo or 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?
Scratch to Python: Moving from Block- to Text-based Programming

Scratch to Python: Moving from Block- to Text-based Programming
Register to receive updates
-
Create an account to receive our newsletter, course recommendations and promotions.
Register for free



