在python中获取指定数量的图像像素

在python中获取指定数量的图像像素,python,opencv,image-processing,Python,Opencv,Image Processing,我有一个代码来获取图像的第一个像素。但是我不能限制像素的数量。我想得到图像的前三个像素,即9 RGB值。这是我的代码 from PIL import Image import numpy as np import cv2 img = cv2.imread('images.jpeg') a=img.shape rows=a[0] cols=a[1] string=raw_input("Enter a message ") l=len(string) m=0 n=0 for i in range(

我有一个代码来获取图像的第一个像素。但是我不能限制像素的数量。我想得到图像的前三个像素,即9 RGB值。这是我的代码

from PIL import Image
import numpy as np
import cv2

img = cv2.imread('images.jpeg')
a=img.shape
rows=a[0]
cols=a[1]
string=raw_input("Enter a message ")
l=len(string)
m=0
n=0
for i in range(0,rows):
    for j in range(0,cols):
        first= img[i,j]
        print first
        break
    break

图像是Python中的2D矩阵。第一个像素位于左上角。但是,很难定义前3个像素。这取决于你。代码如下

import cv2

img = cv2.imread('images.jpeg')        
print img[0,0], img[0,1], img[1,0]

删除这两个break语句是否能满足您的需要?您拥有所有像素,无需循环任何内容。仔细阅读。