Home > Mobile >  Python: How to convert matrix to ASCII string?
Python: How to convert matrix to ASCII string?

Time:12-31

I wanna ask about how to convert matrix to string then convert to text?
For example I have matrices from image and have range from 0 - 255:

[[224 65 90]
[62 125 33]
[75 40 94]]

I want the output is convert all matrix value to ASCII text with string type like this:

'àAZ>}!K(^'

The code:

slices = Image.Image.split(image)
channel_red = np.array(slices[0])

# Matrix to string code below
???

CodePudding user response:

Here is a way to do what you asked:

import numpy as np
m=np.array([[224, 65, 90],
[62 ,125 ,33],
[75 ,40 ,94]])
print(''.join(list(map(chr,m.flatten()))))

Output:

Out[34]: 'àAZ>}!K(^'
  • Related