-
Notifications
You must be signed in to change notification settings - Fork 0
/
asciiToImg.py
62 lines (48 loc) · 1.51 KB
/
asciiToImg.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
# Program: asciiToImg.py
# Written By: Mark Hooper (B00777009)
# Purpose: This script takes a text file and coverts it to
# a 256x256 pixel image. The text is encoded by
# using the ascii value of the characters as either
# the red, green or blue values in the image.
def rgbScrambler(pt,x,y,i):
scrambleType = {
0:(pt,x,y),
1:(y,pt,x),
2:(x,y,pt)
}
return scrambleType.get(i%3)
from PIL import Image
from random import randint
#Create a new Image, and a pixel map.
img = Image.new('RGB',(256,256),color = (0,0,0))
pixels = img.load()
msg = open("msg.txt", "r", encoding = "ASCII")
pt = bytearray(msg.read(), "utf-8")
xPts = [0]
yPts = [0]
for i in range(0, len(pt)):
x = randint(0,254)
y = randint(0,254)
while((x in xPts)and(y in yPts)):
#duplicate pair found, get a new pair
x = randint(0,254)
y = randint(0,254)
#add new unique points to list
xPts.append(x)
yPts.append(y)
pixels[xPts[i],yPts[i]] = rgbScrambler(pt[i],x,y,i)
img.save('msgImg.png')
r,g,b = pixels[0,0]
for i in range(0,len(pt),3):
print(str(r),end = ' ')
print(str(g),end = ' ')
print(str(b))
r,g,b = pixels[g,b]
print(str(r),end = ' ')
print(str(g),end = ' ')
print(str(b))
r,g,b = pixels[b,r]
print(str(r),end = ' ')
print(str(g),end = ' ')
print(str(b))
r,g,b = pixels[r,g]