我正在尝试使用一个完整的RGB值列表创建一个图像,其中每个都是一个像素,我也使用numpy所以我可以通过将其转换为多维数组来编辑列表;但是,当我转换对于数组的列表,包含RGB值的元组将成为包含RGB值的列表,当我完成编辑数组并将其重新转换为列表时,RGB值在列表中而不是元组中,因此无法使用像素。这是我的代码:
from PIL import Image
import numpy
liste=[(134,234,200),(234,0,255)]
new_array = numpy.array(liste)
final_list=new_array.tolist()
im = Image.new("RGB",(10,10))
im.putdata(final_list)
im.show()
最终列表将是这样的:[[134, 234, 200], [234, 0, 255]]
同时我需要的结果是:[(134,234,200),(234,0,255)]
(或听众)
答案 0 :(得分:0)
您可以使用list comprehension将列表列表转换为元组列表,例如:
>>> l = [[1,2,3], [2,3,4], [3,4,5]]
>>> l = [tuple(i) for i in l]
>>> l
[(1, 2, 3), (2, 3, 4), (3, 4, 5)]
答案 1 :(得分:0)
map
from time import clock
spin = 200000
t0 = clock()
for c in range(spin):
li = [[134, 234, 200], [234, 0, 255], [122,12,45], [210,50,87], [3,150,250],
[100,200,0], [230,44,78], [35,78,40], [122,124,231], [251,98,13]]
li = [tuple(e) for e in li]
t1 = clock() - t0
print("%.3f" % t1," li = [tuple(e) for e in li] bgporter")
t0 = clock()
for i in range(spin):
li = [[134, 234, 200], [234, 0, 255], [122,12,45], [210,50,87], [3,150,250],
[100,200,0], [230,44,78], [35,78,40], [122,124,231], [251,98,13]]
li = list(map(tuple,li))
t2 = clock() - t0
print("%.3f" % t2," li = list(map(tuple,li)) eyquem")
print(" %.3f / %.3f = %d %%" % (t2,t1,(100*t2/t1)))
结果:
1.150 li = [tuple(e) for e in li] bgporter
0.938 li = list(map(tuple,li)) eyquem
0.938 / 1.150 = 81 %