将RGB矩阵另存为文本

狮子座

我试图以文本格式保存RGB矩阵,但没有成功。图像的分辨率为640 x480。我正在寻找具有640列和480行的矩阵,并且对于每个元素都需要相应的RGB值。例如:

(230, 200, 20) (130, 11, 13) ... # and the others 658 columns
(200, 230, 20) (11, 130, 13) ... 
... # and the others 478 rows
怪人

如果这是您想要的确切输出,那么我认为这可以完成工作。您可以使用它str.format()来获取所需的任何东西。

# Read the image file.
from scipy import misc
data = misc.imread('image.jpg')

# Make a string with the format you want.
text = ''
for row in data:
    for e in row:
        text += '({}, {}, {}) '.format(e[0], e[1], e[2])
    text += '\n'

# Write the string to a file.
with open('image.txt', 'w') as f:
    f.write(text)

请注意,某些图像类型(例如PNG)通常每个像素包含四个值,因为它们可以具有“ alpha”(不透明度)通道。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章