42

Suppose I have a list of pixels (represented as tuples with 3 RGB values) in a list that looks like list(im.getdata()), like this:

[(0,0,0),(255,255,255),(38,29,58)...]

How do I create a new image using RGB values (each tuple corresponds to a pixel) in this format?

Thanks for your help.

1
  • I tried using im.paste() but it gave me the following error: "SystemError: New style getargs format but argument is not a tuple"
    – Amit
    Aug 21, 2012 at 20:59

3 Answers 3

63

You can do it like this:

list_of_pixels = list(im.getdata())
# Do something to the pixels...
im2 = Image.new(im.mode, im.size)
im2.putdata(list_of_pixels)
0
14

You can also use scipy for that:

#!/usr/bin/env python

import scipy.misc
import numpy as np

# Image size
width = 640
height = 480
channels = 3

# Create an empty image
img = np.zeros((height, width, channels), dtype=np.uint8)

# Draw something (http://stackoverflow.com/a/10032271/562769)
xx, yy = np.mgrid[:height, :width]
circle = (xx - 100) ** 2 + (yy - 100) ** 2

# Set the RGB values
for y in range(img.shape[0]):
    for x in range(img.shape[1]):
        r, g, b = circle[y][x], circle[y][x], circle[y][x]
        img[y][x][0] = r
        img[y][x][1] = g
        img[y][x][2] = b

# Display the image
scipy.misc.imshow(img)

# Save the image
scipy.misc.imsave("image.png", img)

gives

enter image description here

2
  • Scipy only installs the image functions if you have PIL installed in the first place :( Oct 8, 2016 at 18:38
  • 1
    nice picture... I love it! Sep 2, 2021 at 5:58
7

Here's a complete example since I didn't get the trick at first.

from PIL import Image

img = Image.new('RGB', [500,500], 255)
data = img.load()

for x in range(img.size[0]):
    for y in range(img.size[1]):
        data[x,y] = (
            x % 255,
            y % 255,
            (x**2-y**2) % 255,
        )

img.save('image.png')

output

And if you're looking for grayscale only, you can do Image.new('L', [500,500], 255) and then data[x,y] = <your value between 0 and 255>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.