简体   繁体   English

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

[英]getting specified number of pixels of an image in python

I have a code to get the first pixel of an image. 我有一个代码来获取图像的第一个像素。 But I can't limit the number of pixel numbers. 但是我不能限制像素数量。 I want to get the first three pixels of an image ie 9 RGB value.Here is my code . 我想获取图像的前三个像素,即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

Image is a 2D matrix in Python. 图像是Python中的2D矩阵。 The first pixel is the one at top left corner. 第一个像素是左上角的像素。 However, it is hard to define the first 3 pixels. 但是,很难定义前3个像素。 It is up to you. 它是由你决定。 The code is as follows. 代码如下。

import cv2

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM