Created
August 26, 2010 00:44
-
-
Save can3p/550578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys, os, math | |
from PIL import Image | |
def usage(): | |
print ("Usage:") | |
print ("\tcompress input_file out_image") | |
print ("\tcompress -d image out_file") | |
if len(sys.argv) == 1: | |
usage() | |
exit(0) | |
if(sys.argv[1] == '-d'): | |
operation = 'decompress' | |
idx = 1 | |
else: | |
operation = 'compress' | |
idx = 0 | |
if len(sys.argv) < (idx + 3): | |
usage() | |
exit(0) | |
inFile = sys.argv[idx + 1] | |
outFile = sys.argv[idx + 2] | |
if not os.path.exists(inFile): | |
print ("input file does not exist") | |
exit() | |
def encodeDataRGB(str, outFile): | |
print("length of input file: %s" % len(str)) | |
size = int ( math.ceil( math.sqrt(len(str) / 3) ) ) | |
#size = int( math.ceil( math.sqrt(len(str)) )) + 1 | |
#print("width of picture = %s, number of pixels = %s" % (size, size * size)) | |
print("width of picture = %s, number of pixels = %s, number of bytes = %s" % (size, size * size, size * size * 3)) | |
im = Image.new("RGB", ( size, size ), (0x00)) | |
ending = len(str) % 3 | |
datalen = len(str) + (3 - ending) | |
if(ending > 0): | |
str = str + chr(0xFF) + chr(0x00)*(3-ending - 1) | |
data = []; | |
for i in range(int( datalen / 3)): | |
idx = i * 3 | |
byte = (ord(str[idx]), ord(str[idx+1]), ord(str[idx+2])) | |
data.append(byte) | |
data.append((0xFF,0xFF,0xFF)); #we set last byte to 0xFF to mark the end of data | |
im.putdata(data) | |
im.save(outFile) | |
def encodeDataGrayScale(str, outFile): | |
print("length of input file: %s" % len(str)) | |
#size = math.ceil( math.sqrt(len(str) / 3) ) | |
size = int( math.ceil( math.sqrt(len(str)) )) + 1 | |
print("width of picture = %s, number of pixels = %s" % (size, size * size)) | |
#print("width of picture = %s, number of pixels = %s, number of bytes = %s" % (size, size * size, size * size * 3)) | |
im = Image.new("L", ( size, size ), (0x00)) | |
data = []; | |
for i in range(len(str)): | |
byte = ord(str[i]) | |
data.append(byte) | |
data.append(0xFF); #we set last byte to 0xFF to mark the end of data | |
im.putdata(data) | |
im.save(outFile) | |
def decodeDataGrayScale(inFile, outFile): | |
if not os.path.exists(inFile): | |
print("File %s was not found" % inFile) | |
exit() | |
im = Image.open(inFile) | |
data = list(im.getdata()) | |
dataLen = len(data) | |
outStr = "" | |
for offset in range(1, len(data)): | |
if data[-offset] == 0xFF: | |
dataLen -= offset | |
break | |
for px in range(dataLen): | |
outStr += chr(data[px]) | |
f = open(outFile, 'w+') | |
f.write(outStr) | |
if operation == 'compress': | |
input = open(inFile, 'r') | |
encodeDataGrayScale( input.read(), outFile ) | |
else: | |
decodeDataGrayScale( inFile, outFile ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment