Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build an image object in PIL/Python

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?

like image 839
Alex_coder Avatar asked Feb 26 '10 16:02

Alex_coder


1 Answers

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)
like image 188
Peter Hansen Avatar answered Nov 18 '22 08:11

Peter Hansen