I have a variable representing a bmp image with the following structure:
image=[line1, line2, ... linen]
line=[pix1,pix2,...,pixn]
pix=[r,g,b]
thus for instance the contents of this variable may look like
[[[0,0,0],[1,0,1],[2,4,5]],[[3,4,5],[1,7,4],[1,3,5]],[[2,4,2],[1,6,7],[1,9,0]]]
Now I would like to convert the pix-lists of each line-item into tuples, so the contents will become like this:
[[(0,0,0),(1,0,1),(2,4,5)],[(3,4,5),(1,7,4),(1,3,5)],[(2,4,2),(1,6,7),(1,9,0)]]
I could do this by
for i in range(0,len(image)-1):
for j in range(0,len(image[0])-1):
image[i][j] = tuple(image[i][j])
but this doesn’t seem to be the most “pythonesque” way to do it, and I need the fastest computational method possible, which the above seems to be far from.
I tried it with
[tuple(pix) for pix in line for line in image]
as I though this should be faster, but of course the above fails miserably.
So is there anybody who could help me convert my sublists of pix into tuples the fastest way possible? Thanks in advance.
How about this:
And to encompass comments for completeness. In Python 3.x, where map is a generator, you’d use: