Python Challenge level 22: “emulate”

Level 22 got me stuck for a while.

Hint 1: or maybe white.gif would be more bright
Hint 2: image of a joystick

I didn’t know what to do with this one untill I notices it was a GIF89, which reminded me of animated GIFs. After opening it with Gimp I saw that it had 133 frames. Now I had something. It turns out one of the pixels is slightly lighter in each frame in one of several locations. Only 9 of them, actually. Just as the number of possible positions for a joystick. The code bellow is anything but pretty. Beware!

Fact 1: We’re looking for color 8, as shown below. This can be found using im.getcolors() in every frame

#!/usr/bin/env python
import Image,ImageDraw
def get_vectors():
    # Return a list of movement vectors extracted from wiggling pixels
    im = Image.open("white.gif")
    vectors=[]
    try:
        while True:
            pix=list(im.getdata()).index(8)
            y,x=divmod(pix,200)
            v=(x-100,y-100)
            vectors.append(v)
            im.seek(im.tell()+1)
    except EOFError:
        pass # end of sequence
    return vectors

max_x, h, w = 0, 50, 250
im = Image.new('RGB', (w,h))
draw = ImageDraw.Draw(im)
src = (max_x,h//2) # (x,y)
for v in get_vectors():
    if v==(0,0):
        max_x+=30
        src = (max_x,h//2)
        continue
    dst=(src[0]+v[0],src[1]+v[1])
    max_x=max(max_x,dst[0])
    draw.line([src, dst], fill='white')
    src=dst
im.save('22.jpg')

This renders the following image:

bonus

Thats our link for next level: Level 23.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>