[cairo] unable to make PIL work with cairo
Andrea Canciani
ranma42 at gmail.com
Sun Jan 9 02:14:55 PST 2011
On Sun, Jan 9, 2011 at 8:41 AM, wecing <w3cing at gmail.com> wrote:
> This time, I wrote my code like this:
>>>
>>> def video_observer(self, f):
>>>
>>> img = Image.frombuffer('RGB', (self._width, self._height),
>>> f.data, \
>>>
>>> 'raw', 'RGB', 0, 1)
>>>
>>> img = img.resize((self.nwidth, self.nheight))
>>>
>>> img.save('%d.jpg' % self.cnt, 'JPEG') # debug! :-P
>>>
>>>
>>>
>>> imgd = img.tostring('raw', 'RGBA', 0, 1)
>>>
>>> a = array.array('B', imgd)
>>>
>>> surface = cairo.ImageSurface.create_for_data(a,
>>> cairo.FORMAT_ARGB32, \
>>>
>>> self.nwidth, self.nheight, self.nwidth * 4) # self.nwidth * 4
>>>
>>> surface.write_to_png('%d.png' % self.cnt)
>>>
>>> self.context.set_source_surface(surface, 0, 0)
>>>
>>> self.context.paint()
>
>
> But...
>>
>> File "main.py", line 98, in video_observer
>>
>> imgd = img.tostring('raw', 'RGBA', 0, 1)
>>
>> File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 535, in
>> tostring
>>
>> e = _getencoder(self.mode, encoder_name, args)
>>
>> File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 399, in
>> _getencoder
>>
>> return apply(encoder, (mode,) + args + extra)
>>
>> SystemError: unknown raw mode
http://mail.python.org/pipermail/image-sig/2004-October/002985.html
Probably there's no tostring packer for RGB->RGBA either.
Side note: both your questions are actually not related to cairo,
so you might more accurate replies in a PIL mailing list. I'm
basically guessing them based on google results.
Andrea
>
> On Sun, Jan 9, 2011 at 2:37 AM, Andrea Canciani <ranma42 at gmail.com> wrote:
>>
>> On Sat, Jan 8, 2011 at 5:09 PM, wecing <w3cing at gmail.com> wrote:
>> > Currently i'm writing a video player which uses cairo to draw subtitle
>> > on
>> > each frame.
>> > I successfully extracted all the frames as PIL Image objects, but I
>> > failed
>> > to create cairo surfaces with that.
>> > I refered to http://cairographics.org/pythoncairopil/ , but it seems
>> > that
>> > maybe there're some problems in the document.
>> > Here is the traceback:
>> > File "main.py", line 96, in video_observer
>> > img.get_data(), 'raw', 'RGBA', 0, 1)
>> > File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 512, in
>> > __getattr__
>> > raise AttributeError(name)
>> > AttributeError: get_data
>>
>> It looks like you're trying to use the get_data() member of a PIL
>> image (but it doesn't exist). get_data() is a member of cairo images,
>> but to access PIL images raw data you might need something like:
>>
>> data = pil_image.tostring("raw","RGBA",0,1)
>>
>> or (if the format is already correct), just:
>>
>> data = pil_image.getdata() # (no underscore between "get" and "data").
>>
>> See http://www.pythonware.com/library/pil/handbook/image.htm and
>>
>> http://cairographics.org/documentation/pycairo/2/reference/surfaces.html#class-imagesurface-surface
>> for reference.
>>
>> Andrea
>
>
More information about the cairo
mailing list