-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpgmToNumber.py
More file actions
26 lines (26 loc) · 852 Bytes
/
pgmToNumber.py
File metadata and controls
26 lines (26 loc) · 852 Bytes
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
#!/bin/env python3
WIDTH = int(input("File width? "))
HEIGHT = int(input("Picture Height? "))
#WIDTH, HEIGHT = 126,71
outputFile = open("perfectNumber.txt","w")
ORDER = [0,8,6,9,4,5,2,3,1,7]
# I calculated (in pixels per 160, in the order above, for Menlo) 81,79,73,67,65,64,63,60,53,49
try:
with open("input.pgm", "rb") as imageFile:
f = imageFile.read()
b = bytearray(f)
except FileNotFoundError:
print("Please ensure there is a PGM input file named input.pgm")
raise SystemExit
b = b[len(b)-(WIDTH*HEIGHT):]
new = []
for i in b:
float = i * (10/max(b))
if float < 0.5: new.append(ORDER[0])
else: new.append(ORDER[round(float)-1])
output = 0
for i,digit in enumerate(new[::-1]):
output += digit*(10**i)
output = str(output)
output = "0"*((WIDTH*HEIGHT)-len(output)) + output
print(output,file=outputFile)