0

I'm new to Python and I want to create an image from a text file ( it contains tuples of RGB values) That's the code I came up with:

from PIL import Image

img = Image.new( 'RGB', (100,150), "black")
pixels = img.load()

def data():

    plik=open("rgb.txt", 'r')

for i in range(img.size[0]):   
    for j in range(img.size[1]):
        pixels[i,j] = (i, j, data)

img.show()

and I get an error in line pixels[i,j] = (i, j, data). Why?

Hasan Ramezani
  • 5,004
  • 24
  • 30
anu
  • 1
  • In your case, `pixels` isn't an array, it's an image. You can't just treat it like an array. – Gerrat Sep 23 '14 at 18:33

1 Answers1

0

if your text file is in this format:

num11 num12 num13 ....
num21 num22 num23
.
.
.

this code maybe help you:

from PIL import Image
import csv

img = Image.new( 'RGB', (100,150), "black")
pixels = img.load()
reader = csv.reader(open('rgb.txt', 'r'), delimiter=' ')
data = []
for row in reader:
    data.append(tuple([ int(x) for x in row ]))
img.putdata(data)
img.show()
Hasan Ramezani
  • 5,004
  • 24
  • 30