#!/usr/bin/env python
#===================================================================================
#
# FILE: HTS-Programming-02.py
#
# USAGE: HTS-Programming-02.py
#
# DESCRIPTION: This script is for level 2 challenge in HackThisSite
# programming missions.
# _____ _ _ _____ _ _____ _____
# | __||_| _____ ___ | | ___ | _ || | ___ ___ ___ | __| | _ |
# |__ || || || . || || -_| | __|| || .'|| | |___| |__ | _ | __|_
# |_____||_||_|_|_|| _||_||___| |__| |_||__,||_|_| |_____||_||__| |_|
# |_|
#
#===================================================================================
from PIL import Image
img_file = Image.open("/root/HTS/code.png")
img_width, img_height = img_file.size
img_pixel = img_file.load()
preposition=0
morse_code=""
answer=""
char_morse_dict={
'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':'--..','0':'-----','1':'.----','2':'..---','3':'...--',
'4':'....-','5':'.....','6':'-....','7':'--...','8':'---..','9':'----.',
'.':'.-.-.-',',':'--..--','?':'..--..',"'":'.----.','/':'-..-.','(':'-.--.-',
')':'-.--.-',':':'---...',';':'-.-.-.','=':'-...-','+':'.-.-.','-':'-....-',
'_':'..--.-','"':'.-..-.','$':'...-..-','':''
}
# fetch ASCII code from image
for y_point in range(img_height) :
for x_point in range(img_width) :
if img_pixel[x_point, y_point] == 1 :
# convert ASCII code to "dits" and "dahs" in morse code
symbol = chr(x_point + 100 * y_point - preposition)
if symbol != ' ' :
morse_code += symbol
else :
# decode morse code to character
char = [key for key, value in char_morse_dict.iteritems() if value == morse_code][0]
answer += char
morse_code = ""
preposition=x_point + 100 * y_point
# bye bye
print answer