1
from PIL import *

image = ImageGrab.grab()

from the variable image i would like to obtain a list of lists in witch every pixel is rappresented like [R,G,B] every line of the image by the first index of the list and every colum of the image is rappresented by the second index of the list.

for example if we take an image that is only 4*4 pixel, i will like to convert it into a list like this:

[[[R,G,B],[R,G,B],[R,G,B],[R,G,B]],
[[R,G,B],[R,G,B],[R,G,B],[R,G,B]],
[[R,G,B],[R,G,B],[R,G,B],[R,G,B]],
[[R,G,B],[R,G,B],[R,G,B],[R,G,B]]]
2
  • Please first strongly consider whether you wouldn't prefer the data as a Numpy array - Numpy is a common third-party library to install especially if you are already using PIL. If you must have a native Python list I included a duplicate for that as well. Commented Oct 11, 2022 at 4:30
  • The answer is list(image.getdata()). I am providing this answer because even though this question is closed, the specific StackOverflow question still comes up as the top answer on popular search engines for this query. Commented Aug 22, 2023 at 23:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.