我写了一个程序来生成图像像素RGB。
但是它返回了一个错误:
g = pix[x,x][1]
IndexError: image index out of range
这是我的代码:
#!/usr/bin/env python
from __future__ import with_statement
from PIL import Image
im = Image.open('i100m0729.jpg') #relative path to file
#load the pixel info
pix = im.load()
#get a tuple of the x and y dimensions of the image
width, height = im.size
#open a file to write the pixel data
with open('distort_RGB.txt', 'w+') as f:
f.write('X,Y,R,G,B\n')
#read the details of each pixel and write them to the file
for x in range(width):
for y in range(height):
r = pix[x,y][0]
g = pix[x,x][1]
b = pix[x,x][2]
f.write('{0},{1},{2},{3},{4}\n'.format(x+1,y+1,r,g,b))
以下是我正在使用的图片:i100m0729.jpg