LoginSignup
61
66

More than 3 years have passed since last update.

keras.preprocessing.image使い方メモ

Last updated at Posted at 2018-09-04

ページによって引数の説明があったりなかったりするので
実際に動かしたトライをもとに抜粋してメモ

確認環境

python3.6.6
Tensorflow:1.10.0
Keras:2.2.2

import例

from keras.preprocessing.image import load_img, save_img, img_to_array, array_to_img

load_img()

画像をファイルから読み込み、PIL 形式で返す

ex.
load_img(path, grayscale=False, color_mode='rgb', target_size=(224,224))
  • 引数
    • path: 画像を読み込むファイルパス(ex.'***.jpg')
    • grayscale:Trueの場合はGrayscale。Falseの場合はRGB
      ★指示しない場合⇒「RGB」で読込。
    • color_mode: 読み込んだ後に指定した形式に変換する。('rgb', 'rgba', 'grayscale')
      ★指示しない場合⇒「rgb」で読込
    • target_size: リサイズする大きさ
      ★指示しない場合⇒元の画像の大きさ
    • interpolation: リサイズする手法 ('nearest', 'bilinear', 'bicubic')
      ★指示しない場合⇒不明
  • 返り値
    • PIL 形式の画像

備考:grayscaleとcolor_modeだとgrayscale優先の模様

save_img()

ndarray をファイルへ保存する

save_img(path, x)
  • 引数
    • path: 画像を保存するファイルパス(ex.'***.jpg')
    • x: 3次元の ndarray

備考:keras2.2.0から実装

ndarrayを画像に変換し保存するとの事だが、PIL形式正規化したndarrayも「jpeg」で保存された。

img_to_array()

PIL 形式から ndarray に変換する

img_to_array(x)
  • 引数
    • x: PIL 形式の画像
  • 返り値
    • 3次元の ndarray

array_to_img()

ndarray から PIL 形式に変換

array_to_img(x, scale=True)
  • 引数
    • x: 3次元の ndarray
    • scale:Trueで正規化された[0, 1]のデータを[0, 255]に変換
  • 返り値
    • PIL 形式の画像
61
66
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
61
66