0

Does anybody have any tips on how to pack a header and then append it to some chunk of data?

For example, if I have a bit-packed header of 16 bytes, and then I want to append it to a raw RGB image of about 2MB, what is the most efficient way of doing this?

So far, I've tried the following

headerVals = ( str(headerVersion), str(formatEnum), frameCount, ntpTimestamp, width, height )
packedHdr = self.imgStruct.pack( *headerVals )

return packedHdr + data

However this fails with the following error because str and numpy arrays arent concat'able:

    return packedHdr + data
TypeError: cannot concatenate 'str' and 'numpy.ndarray' objects

The only way around this that I can think of, as a Python beginner, is the following which is extraordinarily slow for obvious reasons:

# Generate the header
headerVals = ( str(headerVersion), str(formatEnum), frameCount, ntpTimestamp, width, height )

packDir = 'cchqhh{0}h'.format(width*height*3)

return pack( packDir, str(headerVersion), str(formatEnum), frameCount, ntpTimestamp, width, height, *data )

Any ideas? As a Python initiate I find myself a little stumped by this!

UPDATE:

As per seth's suggestion below, I updated my code to the following, and it is working nicely.

# Generate the header
headerVals = ( str(headerVersion), str(formatEnum), frameCount, ntpTimestamp, width, height )
packedHdr = self.imgStruct.pack( *headerVals )

# Concatanate header to the data
return numpy.concatenate( ( numpy.fromstring( packedHdr, dtype=numpy.uint8 ), data ), axis=0 )
3
  • Why not put headerVersion and formatEnum inside a numpy array, then try to concat? Commented Jul 15, 2013 at 15:27
  • I ended up taking this approach and it seems to be working. Adding changes to question Commented Jul 15, 2013 at 16:04
  • okay, I'll put my comment as an answer. Commented Jul 15, 2013 at 16:48

2 Answers 2

1

To concatenate your header with the numpy array you'll need to have the array in binary form. Assuming data is an array of ints:

import struct

raw_data = packedHdr + struct.pack('i' * data.size, *data)

If your data is of a different kind, you need to specify it in st.pack. You can then unpack the resulting raw_data in the desired form.

Sign up to request clarification or add additional context in comments.

2 Comments

So this works, but I think the efficiency is not great because data is blitted into data.size ints behind the scenes, right?
@bgoldber this will indeed double your data in memory, but it should be put in the same data type as the original numpy array (otherwise you are packing data representations in wrong formats!). For a 2Mb image this is really trivial and the performance issues should be minor, unless you are doing this a lot.
0

Why not put headerVersion and formatEnum inside a numpy array, then try to concat?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.