-
Notifications
You must be signed in to change notification settings - Fork 7
/
mocr.py
134 lines (127 loc) · 3.52 KB
/
mocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python3
# =================================================================================
# Name: mocr.py (Morse OCR)
# Version: v2 (alpha)
# Author: eauxfolles
# Date: 10.07.2020
# Description: Script to read morse code from file and translate into readable text
# Usage: "mocr.py <image>"
# Assumptions: - Morse code expected to be reflected as "." and "-"
# - "." expected to be 1 pixel, "-" expected to be 3 pixel long
# - Image has consistent background color (as pixel at position 0,0)
# - Background color is different than morse code
# - Each line contains one word coded in morse
# =================================================================================
import sys
from PIL import Image
translate = {
'.----': '1',
'..---': '2',
'...--': '3',
'....-': '4',
'.....': '5',
'-....': '6',
'--...': '7',
'---..': '8',
'----.': '9',
'-----': '0',
'.-': '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',
'.-.-.-': '.',
'--..--': ',',
'---...': ':',
'-.-.-.': ';',
'..--..': '?',
'-.-.--': '!',
'-....-': '-',
'..--.-': '_',
'-.--.': '(',
'-.--.-': ')',
'.----.': '\'',
'.-..-.': '\"',
'-...-': '=',
'.-.-.': '+',
'-..-.': '/',
'.--.-.': '@'
}
# validate input provided with command line (has to be 2 parameters or "-help")
if len(sys.argv) < 2:
print("error: no parameters provided")
exit()
elif sys.argv[1] == "-help" or sys.argv[1] == "--help":
print("usage: mocr.py <image>")
exit()
elif len(sys.argv) == 2:
image_file = sys.argv[1]
else:
print("error: wrong number of parameters")
exit()
# use module "PIL" (Python Image Library) to open image-file and load image-data (size and background color)
try:
morse_image = Image.open(image_file)
except:
print("error: could not open file")
exit()
width, height = morse_image.size
pixel_data = morse_image.load()
background_color = pixel_data[0,0]
# define function to translate morse character into letter
def morse_translate(morse_input):
try:
print(translate[morse_input], end = '')
except:
print("\nerror: unable to translate morse code")
exit()
# loop through each pixel, line after line
for line in range(0, height):
morse_char = ""
pixel_count = 0
morse_inline = 0
for pixel in range(0, width):
if pixel_data[pixel, line] != background_color:
pixel_count += 1
morse_inline = 1
else:
if pixel_count == 1:
morse_char += "."
if pixel_data[pixel+1, line] == background_color:
morse_translate(morse_char)
morse_char = ""
elif pixel_count == 3:
morse_char += "-"
if pixel_data[pixel+1, line] == background_color:
morse_translate(morse_char)
morse_char = ""
elif pixel_count == 0:
pass
else:
print("error: cannot read morse code")
pixel_count = 0
if morse_inline != 0:
print("")
# close image-file
morse_image.close()