1. 程式人生 > >python: c_char_p指向的bitmap影象資料,通過c_char_Array最終賦值給PIL的Image物件

python: c_char_p指向的bitmap影象資料,通過c_char_Array最終賦值給PIL的Image物件

    def GetCurrentImage(self):
        ok, bitmap, buff_len = self.GetCurrentFrameBitmap()  #呼叫C函式,返回點陣圖資料的指標. bitmap是c_char_p型別
        if not ok:
            return False,None,'GetCurrentFrameBitmap fail:code=%d, msg=%s'% \
                (reader.LastErrorCode(), reader.LastErrorMessage())
        ret,width,height = self.GetVideoRect()
        if not ret:
            return False,None,'GetVideoRect fail:code=%d, msg=%s'%(reader.LastErrorCode(), reader.LastErrorMessage())
        BitmapType = c_char*buff_len  #建立一個數組物件
        arr = BitmapType()   #陣列的例項
        memmove(arr, bitmap, buff_len)  #拷貝內容
        #img = Image.frombytes('RGB', (width, height), arr)
        img = Image.frombuffer('RGB', (width, height), arr, 'raw', 'RGB', 0, 1)   #陣列直接對映為圖片物件
        return True,img,'success'

這個方法試了三小時才試出來。

求大神指導更好的方法!