端口C的fread(&struct,…)到Python

2024-05-26 22:58:38 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,我真的很纠结于这个问题。我正在尝试将别人的一小段代码移植到Python中,这就是我所拥有的:

typedef struct
{
  uint8_t Y[LUMA_HEIGHT][LUMA_WIDTH];
  uint8_t Cb[CHROMA_HEIGHT][CHROMA_WIDTH];
  uint8_t Cr[CHROMA_HEIGHT][CHROMA_WIDTH];
} __attribute__((__packed__)) frame_t;

frame_t frame;

 while (! feof(stdin))
  {
    fread(&frame, 1, sizeof(frame), stdin);

    // DO SOME STUFF
  }

稍后我需要访问如下数据:frame.Y[x][Y]

所以我在Python中创建了一个类“frame”,并插入了相应的变量(frame.Y,框架Cb, 框架.Cr). 我尝试过按顺序将数据从Y[0][0]映射到Cr[MAX][MAX],甚至打印出了实际的C结构,但没有设法把数据放在那里的方法绕过来。我整晚都在为这件事奋斗,今晚我必须回到军队,所以任何立即的帮助都是非常欢迎和感激的。在

谢谢


Tags: 数据代码框架lumastdinwidthframestruct
1条回答
网友
1楼 · 发布于 2024-05-26 22:58:38

您必须使用structpython标准模块。
根据其文件(增加了Emphasis):

This module performs conversions between Python values and C structs represented as Python strings. It uses format strings (explained below) as compact descriptions of the lay-out of the C structs and the intended conversion to/from Python values. This can be used in handling binary data stored in files or from network connections, among other sources.

注意:由于您最后读取的数据是统一格式的,您也可以使用array模块,然后在Python中“重组”数据,但我认为最好的方法是使用struct。在

相关问题 更多 >

    热门问题