雑多な技術系メモ

自分用のメモ。内容は保証しません。よろしくお願いします。

pythonの画像処理についてのメモ

画像のリサイズ

import cv2

img = cv2.imread("test.jpg", cv2.IMREAD_COLOR)
size = (200, 200)
new_img = cv2.resize(img, size)
cv2.imwrite("new.jpg", new_image)

opencv

画像を読み込む

>>> import cv2
>>> img = cv2.imread("neko.png")
>>> img
array([[[255, 255, 255],
        [255, 255, 255],
        [255, 255, 255],
        ...,
        [255, 255, 255],
        [255, 255, 255],
        [255, 255, 255]]], dtype=uint8)

Pillow で画像をグレー(灰色)に

まず元の画像

>>> from PIL import Image
>>> img = Image.open("./crowncat4020138_TP_V.jpg")
>>> plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f62e821bac8>

f:id:ttt242242:20190826082259p:plain

グレーにする

>>> img_gray = img.convert("LA")
>>> plt.imshow(img_gray)
<matplotlib.image.AxesImage at 0x7f62e8043160>

f:id:ttt242242:20190826082316p:plain

numpy.array形式のデータから画像を表示する。

画像はopen ai gymのゲーム

>>> sample
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       ...,

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        ...,
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]], dtype=uint8)
>>> plt.imshow(sample)
<matplotlib.image.AxesImage at 0x7f62ea6839e8>

f:id:ttt242242:20190826080304p:plain