Python Opencv图片创建+读取像素

import cv2
import numpy as np

def create_img():
    img = np.zeros((3, 4, 3), dtype=np.uint8)
    img_shape = img.shape
    print("row: %d   col: %d  channel:  %d" %(img_shape[0], img_shape[1], img_shape[2]))

    count = 0
    for row in range(img_shape[0]):
        for col in range(img_shape[1]):
            for channel in range(img_shape[2]):
                if channel == 2:
                    # img[row, col, channel] = 255 # 生成红色图片
                    img[row, col, channel] = count
                    count += 1

    cv2.imwrite("E:\\opencvPhoto\\photo\\pythonCount.png", img);


def read_imag():
    img = cv2.imread("E:\\opencvPhoto\\photo\\pythonCount.png")
    img_shape = img.shape
    print("row: %d   col: %d  channel:  %d" %(img_shape[0], img_shape[1], img_shape[2]))
    for row in range(img_shape[0]):
        for col in range(img_shape[1]):
            channels = ""
            for channel in range(img_shape[2]):
                channels = channels + str(img[row, col, channel])
            print(channels)

if __name__ == "__main__":
    read_imag()
    # create_img()
发布了90 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/wangwenjie1997/article/details/105133557
今日推荐