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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章