【Python】【OpenCV】n*m のランダムな色の画像を作成

# vim: set fileencoding=utf-8 :
import numpy as np
import cv2
from random import randint
import numpy as np

cols = 320
rows = 320

#イメージ生成
image = np.zeros((rows, cols, 3), np.uint8)

div = 16 # 縦横の分割数 
w = cols / div # 分割された領域の横幅
h = rows / div # 分割された領域の縦幅
for segrow in xrange(div):
    y1 = segrow * h # 分割領域上
    y2 = y1 + h     # 分割領域下
    for segcol in xrange(div):
        x1 = segcol * w #分割領域左
        x2 = x1 + w     #分割領域右
        b = randint(0,255)
        g = randint(0,255)
        r = randint(0,255) 
        c1 = np.array([b,g,r])
        #(x1,y1)-(x2,y2)の矩形を塗りつぶす
        image[y1:y2, x1:x2] = c1


# 表示して[ESC]が押されるまで待つ
cv2.imshow("image", image)
while cv2.waitKey(33) != 27:
    pass

参考
takamints.hatenablog.jp