How write image buffer to file in python game engine bge 2.62 ?

I try to capture from camera with bge.Texture.VideoFFmpeg, and I want to save frame to file.raw ( byte by byte)… for some programe can process it . ( I don’t know how embed or make other C program share the same buffer with blender, if u know please share me idea :slight_smile: )

with update() module :


from bge import texture
from bge import logic

def update():
    if hasattr(logic, 'video'):
        logic.video.refresh(True)
       
        bufout = texture.imageToArray(logic.video.source,"RGB1")
        #write to file cam1.raw
        output = open("/home/tmp/cam1.raw", "wb")
        output.write(bufout)                          #line 37
        output.close()

Capture run well, eccept it alway throw error : line 37, in update TypeError: ‘NoneType’ does not support the buffer interface.
cam1.raw has 0 byte :frowning:
I tried with property “image” (imageData) of VideoFFmpeg but it throws same err .
I using blender 2.62.
How write buffer or image Data of frame to file with python in blender 2.62 :-?.

You’d have to use the source.image object. Which is just an array of pixels+color channels. It’s possible to save that since you can read it but it’s very slow and unreliable. You never know when the object is filled. You can easily miss the moment it’s filled. This is something that can only be fixed in the C source. However that is based on my VideoFFmpeg experience…

Rg,

Arnaud

I recently posted code about how to write an image to file, solution is for TGA 32bit:

import bpy 
import struct, time 

def write_tga(image_index, filepath): 
    
    with open(filepath, "wb") as f: 
    
        try: 
            img = bpy.data.images[image_index] 
        except IndexError: 
            print("Error: bpy.data.images[%i] does not exist!" % image_index) 
            return 
        
        w, h = img.size 
        
        f.write(struct.pack("<HBIIBHHBB", 0, 2, 0, 0, 0, w, h, 32, 8)) 
        f.write(bytes([int(pix*255) for pix in img.pixels])) 

        f.close() 

filepath = "D:\\imgg.tga" 

t = time.clock() 
write_tga(0, filepath) 
print("Time: %.2f" % (time.clock() - t))

read more at:
http://www.blender.org/forum/viewtopic.php?t=24348