tags:

views:

61

answers:

1

I have a list of 3-item tuples that is the result of list(PIL.Image.getdata()).

How do I do the opposite: build a PIL.Image object from this list?

A: 

The output of getdata() does not include the image format or the size, so you'll need to preserve those (or get the information some other way). Then do this, using the putdata() method:

# get data from old image (as you already did)
data = list(oldimg.getdata())

# create empty new image of appropriate format
newimg = Image.new(format, size)  # e.g. ('RGB', (640, 480))

# insert saved data into the image
newimg.putdata(data)
Peter Hansen
For my scenario, I know the image size apriori, so I didn't include that detail in my question. I agree that your comment about this is valid, a list is unidimensional after all, so the PIL internals must be told how to treat the list as bidimensional array. Thank you.
Alex_coder