実験で取得した画像をCNNに入れる為に、全ての実験フォルダの画像の枚数をそろえる必要があり、
各フォルダの一番最後の画像をコピーしてそれぞれ必要な枚数付け足す、ということをしました。
その覚え書きです。
画像が入ったディレクトリは名前に全て「Motion」が付いているものとします。
各画像名は000.png,001.pngのように三ケタの連番になっているものとします。
このプログラムを同じディレクトリに入れ、TARGETに必要な枚数入れれば実行できます。
-要点
os.getcwd()
: カレントディレクトリの取得
os.listdir(ABC)
:ディレクトリABC内のフォルダとファイルの一覧を取得
re.search('abc',file)
:フォルダに[abc]が入っているか検証
os.chdir(DEF)
:ディレクトリDEFに移動
copy_im=GHI.copy()
:画像GHIをコピー
draw=ImageDraw.Draw(copy_im)
copy_im.save("%03d.png" % (i+count+1),quality=100)
:画像を張り付けし保存。(名前は続きの番号になるようにしてあります。)
-プログラム全文
arrange_pic.py
from PIL import Image
from PIL import ImageFont,ImageDraw
import os
import re
TARGET=230 #put the number you want
# get the path of current directry
dir=os.getcwd()
# loop for the number of Motion file
files=os.listdir(dir)
for file in files:
index=re.search('Motion',file)
if index:
os.chdir(dir+"/"+file)
under_files=os.listdir(dir+"/"+file)
print (dir+"/"+file)
#count the number of pictures
count=0
for file in under_files:
under_index=re.search('.png',file)
if under_index:
count=count+1
#copy the last picture
print count
im=Image.open("%03d.png" % count)
for i in range(TARGET-count):
copy_im=im.copy()
draw=ImageDraw.Draw(copy_im)
copy_im.save("%03d.png" % (i+count+1),quality=100)