Reputation: 85
I have data of RGB values for an image of size 1280x720 in a list that looks like this:
[[102, 107, 111],
[101, 106, 110],
[100, 105, 109],
[100, 105, 109],
[101, 106, 109],
[103, 108, 111],
[105, 110, 113],
...]
I got this data from Image.getdata()
and converted it to a 2d list for separate reasons (this list is called all_data
). However, when I try to set the size to what I want using the code below, I get a TypeError: too many data entries
.
from PIL import Image
import numpy as np
image_out = Image.new(mode = "RGB", size = (1280, 720))
image_out.putdata(all_data)
image_out.show()
This is probably not the right way of doing this, so if anyone has any knowledge of where I should go with it, please let me know. As expected, I have data for 1280*720 pixels that should be in the order that Image.getdata()
puts them in (from what I'd assume, it is a standard left to right and top to bottom rendering).
Upvotes: 0
Views: 805
Reputation: 196
So, the image library requires rgb data to be tuples, not a list. I wasn't able to find this explicitly mentioned in the documentation, but verified it through my own testing. All of the documentation examples also use either ints or tuples (https://hhsprings.bitbucket.io/docs/programming/examples/python/PIL/Image__class_Image.html#id1)
The conversion from a list of lists to a list of tuples can be done by this line:
tuples = [tuple(x) for x in all_data]
In context:
from PIL import Image
import numpy as np
image_out = Image.new(mode = "RGB", size = (1280, 720))
tuples = [tuple(x) for x in all_data]
image_out.putdata(tuples)
image_out.show()
Upvotes: 1