Hassanito Hassanito - 4 months ago 6
Python Question

Im trying to create an image while using a list full of RGB values

I am trying to create an image using a list full of RGB values where every one of them is a pixel ,I am also using numpy so i can edit the list by transforming it to a multidimensional array ;However, when i transform the list to an array , the tuples that contain the RGB values becomes a list that contains RGB values and when i finish editing the array and re transform it to a list the RGB values are in a list instead of a tuple and thus cannot be used as pixels.Here is my code:

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()


the final list will be like that :
[[134, 234, 200], [234, 0, 255]]

meanwhile the result i need is :
[(134,234,200),(234,0,255)]
(or the liste)

Answer

You can use a list comprehension to convert a list of lists into a list of tuples, like:

>>> 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)]
Comments