commit 697dbe2a0019c365b9753a42374e51abb99b7207 Author: Skye Stevenson Date: Sun Mar 25 11:55:30 2018 -0700 Added working grayscale, RGB versions of the script. Functions properly. diff --git a/DataToPixel_Grayscale.py b/DataToPixel_Grayscale.py new file mode 100644 index 0000000..4fd9286 --- /dev/null +++ b/DataToPixel_Grayscale.py @@ -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") \ No newline at end of file diff --git a/DataToPixel_RGB.py b/DataToPixel_RGB.py new file mode 100644 index 0000000..e3818e4 --- /dev/null +++ b/DataToPixel_RGB.py @@ -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") \ No newline at end of file diff --git a/PixelToData_Grayscale.py b/PixelToData_Grayscale.py new file mode 100644 index 0000000..94edcc9 --- /dev/null +++ b/PixelToData_Grayscale.py @@ -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() \ No newline at end of file diff --git a/PixelToData_RGB.py b/PixelToData_RGB.py new file mode 100644 index 0000000..c3c6be7 --- /dev/null +++ b/PixelToData_RGB.py @@ -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() \ No newline at end of file