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

August 02, 2016
Written by
Sam Agnew
Twilion

PokemonFauxBanner

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:

pip install twilio==5.4.0 Flask==0.11.1 requests==2.10.0 Pillow==3.3.0

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):

from PIL import Image


def overlay(original_image_path, pokemon):

    overlay_image = Image.open('pokemon-go-images/overlay.png')

    # This is the image the user sends through text.
    background = Image.open(original_image_path)

    # Resizes the image received so that the height is always 512px.
    base_height = 512.0
    height_percent = base_height / background.size[1]
    width = int(background.size[0] * height_percent)

    background = background.resize((width, int(base_height)), Image.BILINEAR)

    # Resize the overlay.
    overlay_image = overlay_image.resize(background.size, Image.BILINEAR)

    # Specify which pokemon sprite is used.
    pokemon_img = Image.open('pokemon-go-images/{}.png'.format(pokemon))

    # Convert images to RGBA format.
    background = background.convert('RGBA')
    overlay_image = overlay_image.convert('RGBA')
    pokemon_img = pokemon_img.convert('RGBA')

    new_img = background
    new_img.paste(overlay_image, (0, 0), overlay_image)

    # Place the pokemon sprite centered on the background + overlay image.
    new_img.paste(pokemon_img,
                  (int(width / 4), int(base_height / 4)),
                  pokemon_img)

    # Save the new image.
    new_img.save(original_image_path,'PNG')

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:

from overlay import overlay
overlay('path/to/image', 'mewtwo')

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:

import requests
from flask import Flask, request, send_from_directory
from twilio import twiml

from overlay import overlay

UPLOAD_FOLDER = '/Path/to/your/code/directory'
legendary_pokemon = ['articuno', 'zapdos', 'moltres', 'mewtwo', 'mew']

app = Flask(__name__)


@app.route('/sms', methods=['POST', 'GET'])
def sms():
    # Generate TwiML to respond to the message.
    response = twiml.Response()
    response.message("Please wait while we try to catch your Pokemon")

    if request.form['NumMedia'] != '0':

        # Default to Mew if no Pokemon is selected.
        if request.form['Body']:
            # Take the first word they sent, and convert it to lowercase.
            pokemon = request.form['Body'].split()[0].lower()
            if not pokemon in legendary_pokemon:
                pokemon = 'mew'
        else:
            pokemon = 'mew'

        # Save the image to a new file.
        filename = request.form['MessageSid'] + '.png'
        with open('{}/{}'.format(UPLOAD_FOLDER, filename), 'wb') as f:
           image_url = request.form['MediaUrl0']
           f.write(requests.get(image_url).content)

        # Manipulate the image.
        overlay('{}/{}'.format(UPLOAD_FOLDER, filename), pokemon)

        # Respond to the text message.
        with response.message() as message:
            message.body = "{0}".format("Congrats on the sweet catch.")
            message.media('http://{your_ngrok_url}/uploads/{}'.format(filename))
    else:
        response.message("Send me an image that you want to catch a Pokemon on!")

    return str(response)


@app.route('/uploads/', methods=['GET', 'POST'])
def uploaded_file(filename):
    return send_from_directory(UPLOAD_FOLDER, filename)

if __name__ == "__main__":
    app.run()

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/ 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:

ngrok http 5000

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.