0

I have a DLL that returns a pointer to an image. In C#, I can successfully test the pointer using the code below:

car_image_pointer = new System.IntPtr(car_image);

car_pic = (Bitmap)new Bitmap(frame_w, frame_h, frame_step, PixelFormat.Format24bppRgb, plate.car_image_pointer);

However, when attempting to implement similar functionality in Python, I encounter errors such as "not enough image data" or "cannot decode image data." Below is the Python code I have written:

img_width = 1280
img_height = 720
img_step = 3840

img_pointer = c_void_p(pic_pointer)

pixel_format = 24

class Bitmap(Structure):
    _fields_ = [
        ("width",c_int),
        ("height",c_int),
        ("step",c_int),
        ("format",c_int),
        ("scan0",c_void_p),
    ]

bitmap = Bitmap(img_width,img_height,img_step,pixel_format,img_pointer)

bitmap_ptr = pointer(bitmap)

image_data = cast(bitmap_ptr.contents.scan0,POINTER(c_uint8))

IMG = Image.frombuffer("RGB",(img_width,img_height),image_data,"raw")
IMG.save('test.bmp')

base on this(https://stackoverflow.com/a/4355701/16387988) answer i write this code but not work this code too

    img_pointer = c_void_p(car_pic_pointer)

    img_width = 1280
    img_height = 720


    buffer_from_memory = ctypes.pythonapi.PyMemoryView_FromMemory
    buffer_from_memory.restype = ctypes.py_object
    buffer = buffer_from_memory(img_pointer, 8*array_size)

   
    array = np.frombuffer(buffer, float)
    IMG = Image.fromarray(array)
    IMG = IMG.convert('RGB')
    IMG.save('test.png')
md.119
  • 29
  • 7

0 Answers0