Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: How to convert matrix to ASCII string?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

‘àAZ>}!K(^’

The code:

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

# Matrix to string code below
???

>Solution :

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(^'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading