Search code examples
pythontkinterpython-imaging-library

how to take list of pixel rgb values and turn into image


I have a code like this

from PIL import Image

def get_image(image_path):
    image = Image.open(image_path).convert("L")
    image_rgb = image.convert("RGB")
    pixel_values = list(image_rgb.getdata())

    return pixel_values
print(get_image('./test.png'))

and it returns colors of pixels to the output like this

[(r,g,b), (r,g,b), (r,g,b)] 
#^ this is what it would look like if it was from an image made up of three pixels

remember that all of these are inside of the list(image.getdata()) list. im making a program that is going to change some values inside of that list based off of other things. I want to take the edited rgb values and turn them into an image and save the new image to a file


Solution

  • x = 0
    y = 0
    img = Image.open('img.foo')
    pix = img.load()
    for rgb in cr: # cr is the list of rgb values for each pixel
        x = x + 1
        if x > c[1] - 1: # < c[1] returns the max x value
            x = x - c[1]
            y = y + 1
        if y > c[2] - 1: # c[2] returns the max y value
            y = y - c[2]
        pix[x, y] = rgb