我正在尝试将 file.txt 中的txt RGB数据转换为数组。然后,使用该阵列将RGB阵列转换为图像。 (在以下github存储库中找到RGB数据:IR Sensor File.txt)。
我正在尝试将.txt文件转换为可以使用PIL / Image库并将其转换为Image的数组,然后通过以下脚本将其放入以创建我的图像。
我现在的障碍是将file.txt中的数组转换为合适的格式以使用Image函数。
from PIL import Image
import numpy as np
data = [ARRAY FROM THE file.txt]
img = Image.fromarray(data, 'RGB')
img.save('my.png')
img.show()
RGB数据如下所示,也可以在上面链接的github存储库的.txt文件中找到:
[[(0,255,20),(0,255,50),(0,255,10),(0,255,5),(0,255,10),(0,255,25),(0,255,40),(0,255,71),(0,255,137),(0,255,178),(0,255,147),(0,255,158),(0,255,142),(0,255,163),(0,255,112),(0,255,132),(0,255,137),(0,255,153),(0,255,101),(0,255,122),(0,255,122),(0,255,147),(0,255,66),(0,255,66),(0,255,30),(0,255,61),(0,255,0),(0,255,0),(0,255,40),(0,255,66),(15,255,0),(0,255,15)],
[(0,255,40),(0,255,45),(15,255,0),(20,255,0),(10,255,0),(35,255,0),(0,255,5),(0,255,56),(0,255,173),(0,255,168),(0,255,153),(0,255,137),(0,255,158),(0,255,147),(0,255,127),(0,255,117),(0,255,142),(0,255,142),(0,255,122),(0,255,122),(0,255,137),(0,255,137),(0,255,101),(0,255,66),(0,255,71),(0,255,61),(0,255,25),(0,255,25),(0,255,61),(0,255,35),(0,255,0),(35,255,0)],
[(0,255,15),(0,255,25),(51,255,0),(71,255,0),(132,255,0),(101,255,0),(35,255,0),(0,255,20),(0,255,91),(0,255,153),(0,255,132),(0,255,147),(0,255,132),(0,255,158),(0,255,122),(0,255,132),(0,255,142),(0,255,158),(0,255,122),(0,255,137),(0,255,142),(0,255,147),(0,255,101),(0,255,101),(0,255,86),(0,255,86),(0,255,50),(0,255,45),(0,255,50),(0,255,56),(0,255,30),(56,255,0)],
[(0,255,45),(0,255,10),(76,255,0),(127,255,0),(132,255,0)]]
答案 0 :(得分:1)
我认为这应该有效-不知道它是否像Python:
#!/usr/local/bin/python3
from PIL import Image
import numpy as np
import re
# Read in entire file
with open('sensordata.txt') as f:
s = f.read()
# Find anything that looks like numbers
l=re.findall(r'\d+',s)
# Convert to numpy array and reshape
data = np.array(l).reshape((24,32,3))
# Convert to image and save
img = Image.fromarray(data, 'RGB')
img.save('result.png')
我随后放大并对比了图像,以便您可以看到它!