coo*_*ter 0 python numpy python-imaging-library genfromtxt
我正在尝试将 csv 文件转换为 RGB 文件,如下所示:
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')
Run Code Online (Sandbox Code Playgroud)
问题是倒数第二行给了我一个“IndexError:图像索引超出范围”。当我打印出 x 和 y 时,我发现只要它们中的任何一个达到 5(我的文件的宽度,而不是高度),就会发生这种情况,无论我如何使用它,无论 x 或 y 达到 5,它都会引发错误这就是我不明白的。也欢迎任何更好的实现。
如果有人想要工作算法,这里是:
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')
Run Code Online (Sandbox Code Playgroud)
这只是用 temp[y,x] 更改 temp[x,y] 并交换列和行的问题。