views:

252

answers:

2

My app reads frames from video (using pyglet), extracts a 'strip' (i.e. region - full width by 1 to 6 video lines), then pastes each strip (appends it) into an image that can later be written out an *.png file.

Problem is Pyglet GL images are stored in graphics memory, so are very limited re size. My current klunky workaround is build up the to-be png image as small tiles (i.e. within the pyglet GL size limit), when the tile is full, write it out to a *png file, read that back in as a PIL Image file, then paste/append the tile image to the final PIL Image * png file.

I reckon it should be possible to convert the pyglet-GL strip to a PIL Image-friendly format, and paste/append that straight into the final PIL Image, thus having no need for the tile business, which greatly complicates things and has performance impact.

But I can't find how to get the strip data into a form that I can paste into a PIL Image. Have seen many requests re converting the other way, but I've never run with the herd.

+1  A: 

I know it's bad form to reply to one's own question, but I worked out an answer which may be useful to others, so here it is...

nextframe = source.get_next_video_frame() # get first video frame while nextframe != None:# returns None at EOF

#
### extract strip data, conver to PIL Image-friendly form, paste itn into travo Image
#
strip = nextframe.get_region(0, (vid_frame_h//2)-(strip_h//2), strip_w, strip_h) # Extract strip from this frame
strip_image_data = strip.get_image_data() 
strip_data = strip_image_data.get_data(strip_image_data.format, strip_image_data.pitch)
strip_Image = Image.frombuffer("RGB", (strip_w, strip_h), strip_data)
travo_image.paste(strip_Image, (0, travo_filled_to, strip_w, travo_filled_to + strip_h)) # paste strip image into travo_image 
travo_filled_to = travo_filled_to + strip_h  # update travo_filled pointer
nextframe = source.get_next_video_frame()    # get next video frame (EOF returns None)

end of "while nextframe != None:"

A: 

thank you very much for putting your answer here . i've been searching for this for days. great job

Moayyad Yaghi