Python – How to create an image in PIL using a list of RGB tuples

pythonpython-imaging-library

Suppose I have a list of pixels (represented as tuples with 3 RGB values) in a list that looks like list(im.getdata()), like this:

[(0,0,0),(255,255,255),(38,29,58)...]

How do I create a new image using RGB values (each tuple corresponds to a pixel) in this format?

Thanks for your help.

Best Answer

You can do it like this:

list_of_pixels = list(im.getdata())
# Do something to the pixels...
im2 = Image.new(im.mode, im.size)
im2.putdata(list_of_pixels)