慕妹3242003
使用Pillow(适用于Python 3.X以及Python 2.7+),您可以执行以下操作:from PIL import Imageim = Image.open('image.jpg', 'r')width, height = im.sizepixel_values = list(im.getdata())现在您拥有所有像素值。如果是RGB或其他模式可以读取im.mode。然后你可以得到像素(x, y):pixel_values[width*y+x]或者,您可以使用Numpy并重塑数组:>>> pixel_values = numpy.array(pixel_values).reshape((width, height, 3))>>> x, y = 0, 1>>> pixel_values[x][y][ 18 18 12]一个完整,简单易用的解决方案def get_image(image_path): """Get a numpy array of an image so that one can access values[x][y].""" image = Image.open(image_path, 'r') width, height = image.size pixel_values = list(image.getdata()) if image.mode == 'RGB': channels = 3 elif image.mode == 'L': channels = 1 else: print("Unknown mode: %s" % image.mode) return None pixel_values = numpy.array(pixel_values).reshape((width, height, channels)) return pixel_values