0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

フォルダの画像枚数をそろえる為の一斉コピー

Last updated at Posted at 2017-10-11

実験で取得した画像を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)
0
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?