gpt4 book ai didi

python - 将 RGB 矩阵保存为文本

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:09 25 4
gpt4 key购买 nike

我试图以文本格式保存 RGB 矩阵,但没有成功。图像的分辨率为 640 x 480。我正在寻找具有 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”(不透明度) channel 。

关于python - 将 RGB 矩阵保存为文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33757706/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com