How to resolve complex struct from a JPEG file using Python?
<p>The tail of the JPEG file contains a complex structure, such as following:</p>

<pre><code>FFD8...........................
...............................
............FFD9 1C01 0000 ....
...............................

struct definations in C file are:
    typedef struct 
    {
        short wYear;
        short wMonth;
        short wDayOfWeek;
        short wDay;
        short wHour;
        short wMinute;
        short wSecond;
        short wMilliseconds;
    }SYSEMTTIME;
    typedef struct
    {
        int nOcrResult;       
        char szPlateText[16]; 
        char szPlateColor[8]; 
        char szCarColor[8];   
        RECT rtPlate;         
    } OCR_PLATE;
    typedef struct
    {
       unsigned int   size;
       unsigned char  nLane[4];
       unsigned char  nImageFalgs[4];
       unsigned int   nRandom[4];
       unsigned char  nIndex[4];
       unsigned char  nImageIndex[4];
       unsigned char  nTotalCount[4];
       unsigned char  nTrigerNow[4];
       unsigned char  nCarSpeed[4];
       unsigned char  nLimitSpeed[4];
       unsigned char  nDelayFrame[4];
       OCR_PLATE      struPlate[4];
       SYSTEMTIME     stTime;
       unsigned int   szFlags;
    } IMAGE_CAPTURE_INFO;
</code></pre>

<p>And in Python, I have wrote some class using ctype library:</p>

<pre><code>        class POINT(Structure):
            _fields_ = [("x", c_int),("y", c_int)]
        class RECT(Structure):
            _fields_ = [("left", c_int),("top", c_int),("right", c_int),
                        ("bottom",     c_int)]
        class OCR_PLATE(Structure):           
            _fields_ = [("nOcrResult", c_int),
                        ("szPlateText", c_char * 16),
                        ("szPlateColor", c_char * 8),
                        ("szCarColor", c_char * 8),
                        ("rtPlate", RECT)]
        class SYSTEMTIME(Structure):    
            _fields_ = [("wYear", c_short),
                        ("wMonth", c_short),
                        ("wDayOfWeek", c_short),
                        ("wDay", c_short),
                        ("wHour", c_short),
                        ("wMinute", c_short),
                        ("wSecond", c_short),
                        ("wMilliseconds", c_short)]
        class IMAGE_CAPTURE_INFO(Structure):   
            _fields_ = [("size", c_uint),
                        ("nLane", c_ubyte * 4),
                        ("nImageFalgs", c_ubyte * 4),
                        ("nRandom", c_uint * 4),
                        ("nIndex", c_ubyte * 4),
                        ("nImageIndex", c_ubyte * 4),
                        ("nTotalCount", c_ubyte * 4),
                        ("nTrigerNow", c_ubyte * 4),
                        ("nCarSpeed", c_ubyte * 4),
                        ("nLimitSpeed", c_ubyte * 4),
                        ("nDelayFrame", c_ubyte * 4),
                        ("struPlate", OCR_PLATE * 4),
                        ("stTime", SYSTEMTIME),
                        ("szFlags", c_uint)]
</code></pre>

<p>But how to read the data from JPEG image file in the form of the above structure?</p>

<p>You need to know the offset at which this struct is positioned in the file.
Assuming it is at the end of the file:</p>

<pre><code>data = open("filename.jpg", "rb").read()
offset = len(data) - sizeof(IMAGE_CAPTURE_INFO)
myStructure = IMAGE_CAPTURE_INFO.from_buffer(data, offset)
</code></pre>

python struct ctype