sorta captcha breaking thing

game 2 of hackthissite.org’s programming challenge

The directions (you have 30 seconds to upload the code)

“The pixels in the above image are numbered 0..99 for the first row, 100..199 for the second row etc. White pixels represent ascii codes. The ascii code for a particular white pixel is equal to the offset from the last white pixel. For example, the first white pixel at location 65 would represent ascii code 65 (‘A’), the next at location 131 would represent ascii code (131 – 65) = 66 (‘B’) and so on.

The text contained in the image is the answer encoded in Morse, where “a test” would be encoded as “.- / – . … -“”

Sample image:

Solution:

#!/usr/bin/env python
from PIL import Image

#load some image information
imagefile = Image.open('./PNG.png')

key = {  ".-":"A",
         "-...":"B",
         "-.-.":"C",
         "-..":"D",
         ".":"E",
         "..-.":"F",
         "--.":"G",
         "....":"H",
         "..":"I",
         ".---":"J",
         "-.-":"K",
         ".-..":"L",
         "--":"M",
         "-.":"N",
         "---":"O",
         ".--.":"P",
         "--.-":"Q",
         ".-.":"R",
         "...":"S",
         "-":"T",
         "..-":"U",
         "...-":"V",
         ".--":"W",
         "-..-":"X",
         "-.--":"Y",
         "--..":"Z",
         ".----":"1",
         "..---":"2",
         "...==":"3",
         "....-":"4",
         ".....":"5",
         "-....":"6",
         "--...":"7",
         "---..":"8",
         "----.":"9",
         "-----":"0",
         }

#imagedata = imagefile.load()
#width,height = imagefile.size
#print imagedata[0,9]

imagelist = list(imagefile.getdata())

plaintext = ""

lastval = 0
thislettercoded = ""
for i in range(0,len(imagelist)):
  #we only care if the spot is white
  if imagelist[i] == 1:
    thischar = chr(i - lastval)
    if thischar == '.' or thischar == '-':
      thislettercoded = thislettercoded + thischar
    else:
      thislettercoded = thislettercoded.strip()
      plaintext = plaintext + key[thislettercoded]
      thislettercoded = ""
    lastval = i

print plaintext

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: