LoginSignup
0
0

More than 3 years have passed since last update.

pythonで機械学習用の画像を増幅する

Last updated at Posted at 2020-05-03

準備

  • imagesフォルダに増幅対象の画像ファイルを拡張子JPGで格納する(サブディレクトリ内でも可)
  • 以下のライブラリをインストールする
pip install pillow

コード

augmentImages.py
# ---
# 1画像ファイルを20ファイルに増幅し、generatedフォルダに格納する
# ---

from PIL import Image, ImageOps
import glob, os

# 増幅対象のファイルを指定
files = glob.glob('images/**/*.JPG', recursive=True)

for i, file in enumerate(files):
    im = Image.open(file)
    print(i + 1, '/', len(files), file, im.format, im.size, im.mode)

    #出力先ディレクトリを作成
    new_dir = 'generated/' + os.path.dirname(file)
    os.makedirs(new_dir, exist_ok=True)

    #幅1000pxになるようにリサイズ
    resize_ratio = float(1000) / im.width
    im = im.resize((int(im.width * resize_ratio), int(im.height * resize_ratio)))

    #オリジナル画像を保存
    im.save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '.JPG', quality=95)

    #-5〜+5度まで回転してそれぞれ保存
    im.rotate(1).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p1.JPG', quality=95)
    im.rotate(2).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p2.JPG', quality=95)
    im.rotate(3).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p3.JPG', quality=95)
    im.rotate(4).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p4.JPG', quality=95)
    im.rotate(5).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-p5.JPG', quality=95)
    im.rotate(-1).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n1.JPG', quality=95)
    im.rotate(-2).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n2.JPG', quality=95)
    im.rotate(-3).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n3.JPG', quality=95)
    im.rotate(-4).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n4.JPG', quality=95)
    im.rotate(-5).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-n5.JPG', quality=95)

    #反転して-5〜+5度まで回転してそれぞれ保存
    ImageOps.mirror(im).rotate(1).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp1.JPG', quality=95)
    ImageOps.mirror(im).rotate(2).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp2.JPG', quality=95)
    ImageOps.mirror(im).rotate(3).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp3.JPG', quality=95)
    ImageOps.mirror(im).rotate(4).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp4.JPG', quality=95)
    ImageOps.mirror(im).rotate(5).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mp5.JPG', quality=95)
    ImageOps.mirror(im).rotate(-1).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn1.JPG', quality=95)
    ImageOps.mirror(im).rotate(-2).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn2.JPG', quality=95)
    ImageOps.mirror(im).rotate(-3).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn3.JPG', quality=95)
    ImageOps.mirror(im).rotate(-4).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn4.JPG', quality=95)
    ImageOps.mirror(im).rotate(-5).save(new_dir + '/' + os.path.splitext(os.path.basename(file))[0] + '-mn5.JPG', quality=95)

関連

Cognitive ServiceのCustom Visionで学習・テストを実行するPythonコード - Qiita

0
0
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
0
0