SHARE
TWEET

converter.py

a guest Jan 13th, 2015 674 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import tkFileDialog
  2. from PIL import Image, ImageOps
  3.  
  4. filename = tkFileDialog.askopenfilename(defaultextension = '.jpg', filetypes = [('all files', '.*'), ('jpg', '.jpg')])
  5. img = Image.open(filename)
  6. r = max(img.size[0]/84.0, img.size[1]/48.0)
  7. newsize = (int(round(img.size[0]/r)), int(round(img.size[1]/r)))
  8. img = img.resize(newsize, resample = Image.LANCZOS)
  9. img = img.convert(mode = '1')
  10. image = Image.new(mode = '1', size = (84, 48), color = "white")
  11. offset = ((84 - img.size[0]) / 2, (48 - img.size[1]) / 2)
  12. image.paste(img, offset)
  13. pix = image.load()
  14. txt = [["0", "0", "0", "0", "0", "0", "0", "0"] for i in xrange(528)]
  15. for x,y in ((x,y) for y in xrange(48) for x in xrange(84)):
  16.   txt[y*11+x/8][x%8] = str(1 ^ 1 & pix[x,y])
  17. txt = ["0x%s"%(hex(int("".join(i), 2))[2:].rjust(2,"0")) for i in txt]
  18. txt = ", ".join(txt)
  19. open(filename+".txt","w").write(txt)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top