雑多な技術系メモ

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

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

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>

f:id:ttt242242:20190826080304p:plain

Ubuntu(Linux)のコマンドメモ

pandocのコマンド

markdown からpdf

pandoc skill.md -o output.pdf -V documentclass=ltjarticle --latex-engine=lualatex -V geometry:margin=1in -N

文字コード関連

文字コードの確認

nkf -g <filename>

文字コードの変換

utf-8に経間

nkf -w --overwrite <filename>

ディレクトリ内の文字コードを一括変換

ディレクトリ内のファイルの文字コードを全部utf-8に変更

nkf -Lu --oc=UTF-8-BOM --overwrite *.* *.*

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'])

出典:https://stackoverflow.com/questions/38105539/how-to-convert-a-scikit-learn-dataset-to-a-pandas-dataset