Reputation: 75
I'm trying to convert a csv file to an RGB file as follows:
from PIL import Image, ImageDraw
from numpy import genfromtxt
g = open('myFile.csv','r')
temp = genfromtxt(g, delimiter = ',')
im = Image.fromarray(temp).convert('RGB')
pix = im.load()
rows, cols = im.size
for x in range(cols):
for y in range(rows):
print str(x) + " " + str(y)
pix[x,y] = (int(temp[x,y] // 256 // 256 % 256),int(temp[x,y] // 256 % 256),int(temp[x,y] % 256))
im.save(g.name[0:-4] + '.jpeg')
The issue is that the second last line is giving me a "IndexError: image index out of range". when I print out x and y I see that it happens whenever either of them reaches 5 which is the width of my file, but not the height, no matter how I play with it, whether x or y reaches 5, it throws an error and that's what I'm not figuring out. Any better implementations are also welcome.
Upvotes: 0
Views: 6187
Reputation: 75
In case anyone wants the working algorithm, here goes:
from PIL import Image, ImageDraw
from numpy import genfromtxt
g = open('myFile.csv','r')
temp = genfromtxt(g, delimiter = ',')
im = Image.fromarray(temp).convert('RGB')
pix = im.load()
rows, cols = im.size
for x in range(cols):
for y in range(rows):
print str(x) + " " + str(y)
pix[x,y] = (int(temp[y,x] // 256 // 256 % 256),int(temp[y,x] // 256 % 256),int(temp[y,x] % 256))
im.save(g.name[0:-4] + '.jpeg')
It was just a matter of changing temp[x,y] with temp[y,x] and swapping cols and rows.
Upvotes: 5