Hassanito Hassanito - 1 year ago 57
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
new_array = numpy.array(liste)
im = Image.new("RGB",(10,10))

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

meanwhile the result i need is :
(or the liste)

Answer Source

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)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download