0

A Pyro4 server running on 32bit Windows machine is serving numpy image data as a string using img.tostring(), the dtype reported before conversion is int32.

The server code looks like:

def getLastPhase(self):
    print("Sending the data now: ")
    print( self.lastPhase.dtype )
    return self.lastPhase.tostring()

The client code looks like:

data = getLastPhase()

The data is received on a Linux machine with len( data ) = 4177920 or precisely the size of the image in bytes (1024x1020 x4).

However, using fromstring( data, dtype='int32' ) results in exception:

ValueError: string size must be a multiple of element size

If int16 is used instead of int32 no exception is raised but the data is nonsense.

Why is this exception raised in the case where string size matches my data size and not raised in the int16 case?

Is there a difference between string in Python under Windows and Linux?

Any ideas on how to overcome this problem will be much appreciated.

Edit: the python version on the Windows machine is 2.7, whereas on the Linux it is 3.6

lazaraza
  • 57
  • 6

1 Answers1

2

The key point is that in Python 2.x, the str type is (sometimes!) a series of bytes and so not interpreted any further unless you explicitly ask it to be so.

In Python 3.x, the str type is interpreted, and as UTF-8 I believe as standard.

Therefore you want, on Python 3.x, to use the byte type.

The natural way to do this is to encode the string to a series of bytes:

fromstring( data.encode('raw_unicode_escape'), dtype='int32' )

As others mention, String to Bytes Python without change in encoding & Python: Convert Raw String to Bytes String without adding escape chraracters

you need to be careful but in this case I understand it is just transformed binary data so we do not expect any Unicode characters outside of the range raw_unicode_escape will successfully de/encode.

So it is okay.