Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

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

$
0
0
Pokemon Faux: Create Fake Pokemon Go Screenshots with python, Flask and Twilio MMS
Pokemon Faux: Create Fake Pokemon Go Screenshots with Python, Flask and Twilio M ...

PokemonGo 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
Pokemon Faux: Create Fake Pokemon Go Screenshots with Python, Flask and Twilio M ...
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 theoverlayfor 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:

The Twilio Python library for generating TwiML to respond to incoming messages Pillow for image manipulation Flask as the web framework for our web application Requests for downloading images from text messages

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

pipinstalltwilio==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 totake the image we want to manipulate and overlay the Pokemon catching screen overit. 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):

fromPILimportImage defoverlay(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')

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

fromoverlayimportoverlay overlay('path/to/image', 'mewtwo')

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


Pokemon Faux: Create Fake Pokemon Go Screenshots with Python, Flask and Twilio M ...
Responding to picture text messages

We need a Twilio phone numberbefore 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:

importrequests fromflaskimportFlask, request, send_from_directory fromtwilioimporttwiml fromoverlayimportoverlay UPLOAD_FOLDER = '/Path/to/your/code/directory' legendary_pokemon = ['articuno', 'zapdos', 'moltres', 'mewtwo', 'mew'] app = Flask(__name__) @app.route('/sms', methods=['POST', 'GET']) defsms(): # 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 pokemonin legendary_pokemon: pokemon = 'mew' else: pokemon = 'mew' # Save the image to a new file. filename = request.form['MessageSid'] + '.png' f = open('{}/{}'.format(UPLOAD_FOLDER, filename), 'wb') f.write(requests.get(request.form['MediaUrl0']).content) f.close() # Manipulate the image. overlay('{}/{}'.format(UPLOAD_FOLDER, filename), pokemon) # Respond to the text message. withresponse.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/<filename>', methods=['GET', 'POST']) defuploaded_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.

Viewing all articles
Browse latest Browse all 9596

Trending Articles