Created
August 11, 2019 09:38
-
-
Save ongspxm/65ac6a98cceb33347640b624982f1f69 to your computer and use it in GitHub Desktop.
using pillow to encode and decode files into images
This file contains 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
import sys | |
from PIL import Image | |
try: | |
ifile = sys.argv[1] | |
ofile = sys.argv[2] | |
except: | |
print ('usage: dec.py [input file] [out file]\n\n') | |
img = Image.open(ifile) | |
fbs = list(img.getdata()) | |
spixel = fbs[0] | |
leng = spixel[0]*255*255 + spixel[1]*255 + spixel[2]; | |
num = int(leng/3)+1 | |
fbs[0] = fbs[num] | |
bs = [] | |
for b in fbs: | |
bs.extend(b) | |
bs = bs[:leng] | |
with open(ofile, 'wb') as f: | |
f.write(bytes(bs)) | |
print('%d bytes extracted into %s'%(leng, ofile)) |
This file contains 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
import sys | |
from PIL import Image | |
try: | |
fname = sys.argv[1] | |
except: | |
print ('usage: enc.py [filename]\n\n') | |
with open(fname, 'rb') as f: | |
bs = list(f.read()) | |
leng = int(((len(bs)/3)+1)**0.5) + 1 | |
img = Image.new('RGB', (leng, leng)) | |
num = int(len(bs)/3)+1 | |
for i in range(num): | |
val = list(bs[i*3:(i+1)*3]) | |
for j in range(3 - len(val)): | |
val.append(0) | |
img.putpixel( (i%leng, int(i/leng)), tuple(val)) | |
val = (int(len(bs)/(255**2)), | |
int(len(bs)%(255**2)/255), len(bs)%255); | |
print('%d bytes written into %s.png'%(len(bs), fname)) | |
img.putpixel((num%leng, int(num/leng)), tuple(bs[:3])) | |
img.putpixel((0,0), val) | |
img.save(fname+'.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment