Skip to content

Instantly share code, notes, and snippets.

@reillysiemens
Last active September 17, 2017 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reillysiemens/5bf68eb62bf1eb1bd423889b6c49c9d6 to your computer and use it in GitHub Desktop.
Save reillysiemens/5bf68eb62bf1eb1bd423889b6c49c9d6 to your computer and use it in GitHub Desktop.
Generate a PNG with the remote IP address in it.

avtar.png

These files are web applications that generate PNGs on request containing a friendly greeting with the IP address of the requester.

The PHP was originally written by Andrew Kvalheim probably around 2010. Its filename at that time was avatar.png, so as to give the illusion that it was a static file.

I wrote the Python many years later in an attempt to understand what Andrew had written. When I first read that code it was black magic to me. Happening by it again after such a long time, it seemed the most appropriate thing to do was to port the PHP to a language I'm very familiar with. The result is a small Flask app that reproduces the original output in a more Pythonic fashion.

I've added some extra comments should anyone else seek to understand what my version is doing.

<?php
//Get IP address
$ip = explode('.' $_SERVER['REMOTE_ADDR'], 4);
//Render image
$image = @imagecreate(256, 256)
or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($image, 119, 41, 83);
$text_color = imagecolorallocate($image, 255, 255, 255);
imagettftext($image, 24, 0, 8, 96, $text_color, 'UbuntuMono-Regular.ttf', "Hello, \n$ip[0].$ip[1].$ip[2].$ip[3]!");
//Send response
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
from io import BytesIO
from flask import Flask, request, send_file
from PIL import Image, ImageFont, ImageDraw
IMAGE_SIZE = (256, 256)
# Ubuntu Light Aubergine https://design.ubuntu.com/brand/colour-palette
BACKGROUND_COLOR = (119, 33, 111)
# Ubuntu Monospace Regular https://design.ubuntu.com/brand/ubuntu-font-family
FONT = 'UbuntuMono-Regular.ttf'
FONT_SIZE = 27
TEXT_COLOR = (255, 255, 255)
TEXT_POSITION = (8, 96)
app = Flask(__name__)
@app.route("/avatar.png")
def avatar():
addr = request.remote_addr
# Create a blank image canvas of the specified size and color.
image = Image.new('RGB', IMAGE_SIZE, BACKGROUND_COLOR)
# Get the specified font.
font = ImageFont.truetype(FONT, FONT_SIZE)
# Get a drawing context for the new image.
draw = ImageDraw.Draw(image)
# Draw text to the image at the specified position in the specified color
# and font.
draw.text(TEXT_POSITION, "Hello, \n{}!".format(addr), TEXT_COLOR, font=font)
# Allocate an in-memory bytes buffer and save the image to it, taking care
# to seek back to the beginning of the image buffer.
image_buffer = BytesIO()
image.save(image_buffer, 'PNG', omptize=True) # Shave a few bytes.
image_buffer.seek(0)
# Send the buffer as a file with the appropriate media type.
return send_file(image_buffer, mimetype='image/png')
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment