Skip to content

Instantly share code, notes, and snippets.

@ongspxm
Created August 11, 2019 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ongspxm/65ac6a98cceb33347640b624982f1f69 to your computer and use it in GitHub Desktop.
Save ongspxm/65ac6a98cceb33347640b624982f1f69 to your computer and use it in GitHub Desktop.
using pillow to encode and decode files into images
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))
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