Python IndexError:图像索引超出范围

时间:2016-07-29 08:21:04

标签: python-2.7 indexing error-handling

我写了一个程序来生成图像像素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

0 个答案:

没有答案