Added working grayscale, RGB versions of the script. Functions properly.

This commit is contained in:
Skye Stevenson 2018-03-25 11:55:30 -07:00
commit 697dbe2a00
4 changed files with 183 additions and 0 deletions

49
DataToPixel_Grayscale.py Normal file
View File

@ -0,0 +1,49 @@
# TODO add feature to include original filename in file, use filename to reconstruct original file without user input
# TODO make this work in RGB colorspace, reduce number of pixels needed
# we need to encode ASCII data as an image!
# each ASCII character takes up a 0-127 space, meaning we can fit at least 1 ASCII character into each channel of a given pixel in an image!
# we need to assign the numeric value of a series of ASCII characters to each of the channels of each pixel of an image!
# make list from string
#fileName = raw_input("Give me a file to pixelize:\n")
fileName = "test.jpg"
data = open(fileName,"r")
dataString = data.read()
#dataString = raw_input("Please give me text:")
dataList = list(dataString)
listLength = len(dataList)
print "list length: " + str(listLength)
import math
width = int(math.ceil(math.sqrt(listLength)))
print "width: " + str(width)
# make the image
from PIL import Image
image = Image.new('L',(width,width),color=0)
# load the temp image and store the ASCII value in one of its pixels
px = image.load()
# px[1,1] = (ascValue)
#image.show()
# we have to loop through the coordinates 1,1 2,1 3,1 1,2 2,2 3,2 with 3 being the width of the image - maybe we raise the x coordinate until it hits the width count, then raise the y coordinate until it hits the secnd width count, then do the same operation - can we use modulo to determine if a scanline width count has been reached?
# then we need to assign the ASCII numeric value to the pixel at each of those locations
index = 0
for q in range(0,width):
for i in range(0,width):
if index < listLength: #prevents an error when trying to access data at indices higher than what's in the list
px[i,q] = (ord(dataList[index]))
# print ord(dataList[index])
print str(index) + "/" + str(listLength) + " done"
index += 1
image.show()
image.save("asciiConverted.png")

64
DataToPixel_RGB.py Normal file
View File

@ -0,0 +1,64 @@
# TODO add feature to include original filename in file, use filename to reconstruct original file without user input
# TODO make this work in RGB colorspace, reduce number of pixels needed
# we need to encode ASCII data as an image!
# each ASCII character takes up a 0-127 space, meaning we can fit at least 1 ASCII character into each channel of a given pixel in an image!
# we need to assign the numeric value of a series of ASCII characters to each of the channels of each pixel of an image!
# make list from string
fileName = raw_input("Give me a file to pixelize:\n")
#fileName = ""
data = open(fileName,"r")
dataString = data.read()
#dataString = raw_input("Please give me text:")
dataList = list(dataString)
listLength = len(dataList)
#print "list length: " + str(listLength)
import math
width = int(math.ceil(math.sqrt(listLength/3)))
#print "width: " + str(width)
# make the image
from PIL import Image
image = Image.new('RGB',(width,width),color=0)
# load the temp image and store the ASCII value in one of its pixels
px = image.load()
# px[1,1] = (ascValue)
#image.show()
# we have to loop through the coordinates 1,1 2,1 3,1 1,2 2,2 3,2 with 3 being the width of the image - maybe we raise the x coordinate until it hits the width count, then raise the y coordinate until it hits the secnd width count, then do the same operation - can we use modulo to determine if a scanline width count has been reached?
# then we need to assign the ASCII numeric value to the pixel at each of those locations
index = 0
for q in range(0,width):
for i in range(0,width):
if index < listLength: #prevents an error when trying to access data at indices higher than what's in the list
# evaluate RGB values
rVal = ord(dataList[index])
if (index+1) < listLength:
gVal = ord(dataList[index+1])
else:
gVal = 0
if (index+2) < listLength:
bVal = ord(dataList[index+2])
else:
bVal = 0
px[i,q] = (rVal,gVal,bVal)
#print rVal
#print gVal
#print bVal
print str(index) + "/" + str(listLength) + " done"
index += 3
image.show()
image.save("asciiConverted_RGB.png")

27
PixelToData_Grayscale.py Normal file
View File

@ -0,0 +1,27 @@
#fileName = raw_input("Give me a file to pixelize:\n")
from PIL import Image
image = Image.open("asciiConverted.png")
# get image width
width, height = image.size
# load up pixels
px = image.load()
stringOutput = ""
index = 0
for q in range(0,width):
for i in range(0,width):
stringOutput += chr(px[i,q])
print str(index) + "/" + str(width^2) + " done"
index += 1
#print stringOutput
# save to file
outFile = open("TESTCONVERT.jpg","w")
outFile.write(stringOutput)
outFile.close()

43
PixelToData_RGB.py Normal file
View File

@ -0,0 +1,43 @@
#fileName = raw_input("Give me a file to pixelize:\n")
from PIL import Image
image = Image.open("asciiConverted_RGB.png")
# get image width
width, height = image.size
# load up pixels
px = image.load()
stringOutput = ""
index = 0
for q in range(0,width):
for i in range(0,width):
# stringOutput += chr(px[i,q])
pixelString = str(px[i,q])
pixelString = pixelString.strip("()")
pixelSplit = pixelString.split(", ")
#print pixelString
#print "---"
for channel in pixelSplit:
#print channel
stringOutput += chr(int(channel))
#print "-"
print str(index) + "/" + str(width*width) + " done"
index += 1
#print stringOutput
# save to file
outFile = open("TESTCONVERT.txt","w")
outFile.write(stringOutput)
outFile.close()