NumPyでランダム画像を生成


このエントリーをはてなブックマークに追加

いつも忘れるのでメモ。値域が0以上255以下で、縦がheight, 横がwidthのランダム画像を生成するには

np.random.randint(256, size=(height, width, 3)).astype(np.uint8)

とします。以下でも同じことです。

np.random.randint(low=0, high=256, size=(height, width, 3)).astype(np.uint8)

以下は一例です。

>>> np.random.randint(256, size=(height, width, 3)).astype(np.uint8)
array([[[153,  62, 250],
        [ 82, 185, 178],
        [ 84, 166,  35],
        [ 56, 184,  51],
        [ 13, 251,  95]],

       [[201, 196,  79],
        [ 98,  54,  85],
        [246, 210, 101],
        [129, 206,   9],
        [ 46,  44,  92]],

       [[ 44, 136,  11],
        [149,  30,  43],
        [153, 164, 173],
        [177,  92, 237],
        [ 53,  47, 237]],

       [[103, 108, 115],
        [242,  38, 244],
        [ 31,  37, 176],
        [ 90,  67, 184],
        [248,  75,  17]]], dtype=uint8)