Pokemon Faux: Create Fake Pokemon Go Screenshots with Python, Flask and Twilio MMS

Pokemon Go is awesome and we all want to show off when we catch rare Pokemon. Let’s build a quick hack using Python and Twilio MMS that will allow you to trick your friends into thinking that you’ve encountered legendary Pokemon.

You can continue reading to find out how to build this, or try it out now by texting an image and the name of a legendary Pokemon to:

(646) 760-3289

articuno_spotted.png

Getting started

Before diving into the code, you’ll first need to make sure you have the following:

  • Python and pip installed on your machine
  • A free Twilio account – sign up here
  • The images we will need to use including screenshots of models of the legendary Pokemon and the overlay for the Pokemon encounter screen. Create a new folder called pokemon-go-images in the directory where you want your project to live and save them there.

The dependencies we are going to use will be:

Open your terminal and enter these commands, preferably in the safety of a virtual environment:

Overlaying images on top of each other

Let’s write some code to take the image we want to manipulate and overlay the Pokemon catching screen over it. We will use the Image module from PIL.

We need a function that takes a path to an image and the name of a Pokemon. Our function will resize the images to be compatible with each other, paste the overlay over the background image, paste the selected Pokemon on the image and then overwrite the original image with the new image.

Open a file called overlay.py and add the following code (comments are included in-line to explain what is happening):

Try running it on your own image. This works best with images taken on phones, but let’s just see if it works for now. Open up your Python shell in the same directory as the file you just created and enter the following two lines:

Now open the new image and see if you are catching a Mewtwo on a train like I am:

mewtwo_spotted.png

Responding to picture text messages

We need a Twilio phone number before we can respond to messages. You can buy a Twilio phone number here.

Now that we have the image manipulation taken care of, make a Flask app that receives picture messages and responds to them with a Pokemon being captured in that picture.

Open a file called app.py in the same directory as before and add the following code:

The /sms route responds to an incoming text message with some Twilio flavored XML called TwiML. Notice that the ‘NumMedia’ parameter is not zero, meaning we received an MMS. Twilio does not return an image itself, they return a URL to the image.

We create a string called filename using the MessageSid parameter to maintain a unique identifier for each image. Then the program opens the file to write the content of Twilio’s image to the file.

The second route, /uploads/<filename> handles the delivery of the message.media TwiML using that URL to retrieve the new image.

Your Flask app will need to be visible from the internet in order for Twilio to send requests to it. We will use ngrok for this, which you’ll need to install if you don’t have it. In your terminal run the following command:

bLqHzdmXrzBH4pEr4ADRoWbU2Kgitkg848ZYE-aRACC0ZGMgC6a_98CmPb9VCzvFSrpbbDc35J_IdTwUShfEqx4zuzJdqthNy9En-RE3-8Ma2h3gXOzA3kErkbqrvjqBZxJg4sZH.png

This provides us with a publicly accessible URL to the Flask app. Configure your phone number as seen in this image:

022N1RISjv9KCUvlhfN4dJtBKJvzP_96q9d558H2YMmpLGOoyl9PSqap29D11yzHgHHJVCY2u6x86xQs37xvSv1ZB3t7vxZEp3rb4n58ZuvwdSkT-ydPBD8DgvCqMmknzPHF3r4w.png

Before testing this out, make sure that you’ve changed the file paths and the URL to reflect your own.

Now try texting an image and the name of a legendary Pokemon to your newly configured Twilio number. Looks like we found a Zapdos in the Twilio New York office!

zapdos_spotted.png

Time to catch ’em all!

Now that you can send messages to make it look like you are catching legendary Pokemon in any arbitrary picture, your quest to making your friends think you are a Pokemon master can truly begin.

The code for this project also lives on this GitHub repository.

For more Pokemon Go-related awesomeness, check out this post that will walk you through setting up SMS alerts when rare Pokemon are nearby.

Feel free to reach out if you have any questions or comments or just want to show off the cool stuff you’ve built.

Thanks to my good friend Shahan Akhter for helping out with image manipulation and tweaking the Python code to make the images look better.