Pillow で画像をグレー(灰色)に
まず元の画像
>>> img = Image.open("./crowncat4020138_TP_V.jpg") >>> plt.imshow(img) <matplotlib.image.AxesImage at 0x7f62e821bac8>
グレーにする
>>> img_gray = img.convert("LA") >>> plt.imshow(img_gray) <matplotlib.image.AxesImage at 0x7f62e8043160>
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>
グレーにする
>>> img_gray = img.convert("LA") >>> plt.imshow(img_gray) <matplotlib.image.AxesImage at 0x7f62e8043160>
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>
numpy.array形式のデータから画像を表示する
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>
Ubuntu(Linux)のコマンドメモ
ImportError: cannot import name 'NavigationToolbar2Tk'
matplotlibが古いの原因だった。
なので、以下のコマンドでupdateすれば大丈夫
pip install -U matplotlib
Scikit-learnのirisデータセットをpandasで扱う
ありがたいことに、stack overflowで回答されている方がいた。
以下irisデータセットをpandasに変換するコード
import numpy as np import pandas as pd from sklearn.datasets import load_iris # save load_iris() sklearn dataset to iris # if you'd like to check dataset type use: type(load_iris()) # if you'd like to view list of attributes use: dir(load_iris()) iris = load_iris() # np.c_ is the numpy concatenate function # which is used to concat iris['data'] and iris['target'] arrays # for pandas column argument: concat iris['feature_names'] list # and string list (in this case one string); you can make this anything you'd like.. # the original dataset would probably call this ['Species'] data1 = pd.DataFrame(data= np.c_[iris['data'], iris['target']], columns= iris['feature_names'] + ['target'])