[英]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:例如,我有来自图像的矩阵,范围为 0 - 255:
[[224 65 90]
[[224 65 90]
[62 125 33][62 125 33]
[75 40 94]][75 40 94]]
I want the output is convert all matrix value to ASCII text with string type like this:我希望 output 将所有矩阵值转换为字符串类型的 ASCII 文本,如下所示:
'àAZ>}!K(^'
'àAZ>}!K(^'
The code:编码:
slices = Image.Image.split(image)
channel_red = np.array(slices[0])
# Matrix to string code below
???
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(map(chr,m.flatten())))
Updated based on rioV8's comment - no need for a list after join
.根据 rioV8 的评论更新 -
join
后不需要列表。
Output : Output :
Out[34]: 'àAZ>}!K(^'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.