Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Created August 31, 2012 02:07
Show Gist options
  • Save nobonobo/3547854 to your computer and use it in GitHub Desktop.
Save nobonobo/3547854 to your computer and use it in GitHub Desktop.
wxPythonでImageを2値化ストリームに変換する方法
from itertools import imap, islice, izip
def make_binstream(img):
def _bytestream(dt):
stream = izip(islice(dt,0,None,3), islice(dt,1,None,3), islice(dt,2,None,3))
while 1:
# bits: 8コのboolアレイ
bits = [stream.next()==('\x00','\x00','\x00') for i in range(8)]
yield chr(reduce(lambda a,b: (a<<1) + b, bits)) # bitsを1バイトに変換
dt = img.ConvertToMono(255,255,255).GetData()
return ''.join(_bytestream(dt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment