Archived

This forum has been archived. Please start a new discussion on GitHub.

Python mapping for byte sequences

Dear All,

I have some problems mapping a byte sequence in Python. The Ice documentation strongly recommends to use Python's string for reading byte sequences. In my case I'm reading a grayscale image from the byte sequence where each pixel is represented by 8 bits as follows:
arr = np.fromstring(bytePixels, dtype=np.uint8)

Then I put the obtained data to the image structure, in the current example depth=1, therefore we have a matrix of height x width:
orig_image_data = np.zeros([height, width, depth], np.uint8)
orig_image_data[:] = np.reshape(arr, orig_image_data.shape)

Despite the reshape works fine and values look reasonable, further operations on the array fail. For example, plotting the array using the Matplotlib I'm receiving the following error message:
TypeError: Invalid dimensions for image data

which is coming from the Matplotlib.

However, if I save the
orig_image_data
to the file
np.savetxt('orig_image', orig_image_data)

and read the data back from the disk as
orig_image_data = np.loadtxt('orig_image', dtype=np.uint8)

everything works fine. Does anybody have any ideas? I assume some problems during the conversion from the byte sequence to the string. I've tried to specify the string size explicitly using the 'count' parameter, but it doesn't make any difference.

Any suggestions are kindly welcome!

Many thanks in advance,
Alexey

Comments

  • mes
    mes California
    Hi Alexey,

    As a simple test, I wrote an Ice for Python application that transfers the contents of an image file from server to client. The client compares the data it received to the contents of the image file, and there are no differences. I even had the client write the data to a new file, just so I could confirm that the files were identical.

    I did this using Ice 3.4.2 on CentOS 6.

    If you still think that Ice is at fault, please tell us your version information (for Ice, OS, Python) and provide a small example that demonstrates the problem.

    Regards,
    Mark
  • Hi Mark,

    thank you for the reply. I'm using Ubuntu 10.04, Ice 3.4.2 and Python 2.6. I'm not sure who exactly causes the problem, but the numpy array is getting unusable after the reading data from the sequence.

    Best,
    Alexey
  • It's fixed now! The problem was that I've been using an array, created as:
    orig_image_data = np.zeros([height, width, spp], np.uint8)
    

    with spp = 1, as a 2-dimensional array. But since its shape property indicates that it's 3-dimensional, it cannot be treated as a 2-dimensional array and its third dimension needs to be addressed explicitly, by "0" in this case.

    Thanks for the help anyway!

    Best,
    Alexey