[Image-SIG] Monochrome image tostring length problems

tom tom at t0mb.net
Tue Nov 14 22:19:47 CET 2006


Hi guys,

I'm having a real problem at the moment trying to figure out how to pass 
strings of 1bit image data between PIL and the g15daemon which supports 
the logitech g15 keyboards lcd.  Basically, it's 160x143 in resolution.  
The g15 python bindings have two methods of note.  The 
loader_10_ascii_format method loads a file which is formatted as 
1001101110001 etc, and converts it in to a string of the form 
\x00\x01\x01\x00 etc.... this string has a len of 6880, which is the 
length it should be, being 160x43. If i try to do anything with a PIL 
created image initialised like

i = Image.new('1', (160, 43))

and then, with s being the string returned from the g15 ascii loader, do 
something like this

i.fromstring(s)

then you have a problem illustrated like so:

len(i.tostring('raw', '1'))
returns 860  (6880 / 8)

len(i.tostring('raw', '1').encode('string-escape')
returns 3440    half the number i need...


I cannot send this string to the g15daemon.set_buffer method as it needs 
to be a string which is 6880 in length

see here for part of my ipython session which illustrates the issue.

In [65]: i = Image.new("1", (160, 43))

In [66]: s = g15daemon.loader_ascii_10_format('lcd.ascii')

In [67]: len s
-------> len(s)
Out[67]: 6880

In [68]: len i.tostring()
-------> len(i.tostring())
Out[68]: 860

In [69]: len i.tostring().encode('string-escape')
-------> len(i.tostring().encode('string-escape'))
Out[69]: 3440

In [70]: i.fromstring(s)

In [71]: len i.tostring()
-------> len(i.tostring())
Out[71]: 860

In [72]: len i.tostring().encode('string-escape')
-------> len(i.tostring().encode('string-escape'))
Out[72]: 3440

In [73]: 160*43
Out[73]: 6880

In [74]: g = g15daemon.g15screen(g15daemon.SCREEN_PIXEL)

In [76]: g.set_buffer(i.tostring())
---------------------------------------------------------------------------
exceptions.Exception                                 Traceback (most 
recent call last)

/home/voidy/<ipython console>

/usr/lib/python2.4/site-packages/g15daemon.py in set_buffer(self, buf, 
use_exceptions)
    121     def set_buffer(self, buf, use_exceptions=True):
    122         """Set a new buffer"""
--> 123         return self.__validate_buffer(buf,use_exceptions)
    124
    125     def set_pixel(self, x, y, pixel):

/usr/lib/python2.4/site-packages/g15daemon.py in __validate_buffer(self, 
buf, use_exceptions)
     95         if len(buf) != MAX_BUFFER_LENGTH:
     96             if use_exceptions:
---> 97                 raise Exception("Buffer has wrong size(%d)" % 
len(buf))
     98             return False
     99         for i in range(0,MAX_BUFFER_LENGTH):

Exception: Buffer has wrong size(860)



Any help would be appreciated so much, i've been almost enraged with 
frustration at this.  I just really want to be able to form my canvas 
with the graphical and textual methods from pil, and then convert it 
over to the lcd's scerenbuffer..

Regards, Tom/



More information about the Image-SIG mailing list