Last active
June 9, 2018 06:39
-
-
Save kyoro1/9ab8c798d2b66f1b448c5b0d9fe3aa26 to your computer and use it in GitHub Desktop.
画像回転スクリプト & 並列処理 ref: https://qiita.com/kyoro1/items/760b56508844a083901c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
from scipy import ndimage | |
import os | |
from joblib import Parallel, delayed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 角度の定義 | |
rIntr = 30 | |
## 回転開始角度 | |
rs = 30 | |
##回転終了角度 | |
re = 330 | |
## input images | |
Image_DIR = '<Replace your path for input images>' | |
## output images | |
output_DIR = '<Replace your path for output images>' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def getFilesInDirectory(directory, postfix = ""): | |
fileNames = [s for s in os.listdir(directory) if not os.path.isdir(os.path.join(directory, s))] | |
if not postfix: | |
return fileNames | |
else: | |
return [s for s in fileNames if s.lower().endswith(postfix)] | |
## mirror inversion | |
def generate_flip(I): | |
fimg = I.copy() | |
return cv2.flip(fimg ,0) | |
## Generate(rotate) images & store for each image | |
def generate_rot(imgFilename, rs, re, rIntr, output_DIR, image_type): | |
I = cv2.imread(imgFilename) | |
## mirror-inversion, if needed | |
if image_type == 'Reverse': | |
I = generate_flip(I) | |
for r in range(rs, re+1, rIntr): | |
## Rotation of each image | |
Irot = ndimage.rotate(I, r, reshape=False) | |
## Store images | |
FILENAME = imgFilename+'_'+str(image_type)+'_'+str(r)+'.jpg' | |
print('Completed for '+str(FILENAME)) | |
cv2.imwrite(os.path.join(output_DIR,FILENAME), Irot) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if __name__ == "__main__": | |
## Input imageのファイル名取得 | |
os.chdir(Image_DIR) | |
imgFilenames = getFilesInDirectory(Image_DIR, ".jpg") | |
## Normal rotation | |
Parallel(n_jobs=-1, verbose=3, | |
backend="threading" | |
)([delayed(generate_rot)(imgFilename=imgFilename, | |
rs=rs, | |
re=re, | |
rIntr=rIntr, | |
output_DIR=output_DIR, | |
image_type='Normal') | |
for imgFilename in imgFilenames]) | |
## Reverse rotation | |
Parallel(n_jobs=-1, verbose=3, | |
backend="threading" | |
)([delayed(generate_rot)(imgFilename=imgFilename, | |
rs=rs, | |
re=re, | |
rIntr=rIntr, | |
output_DIR=output_DIR, | |
image_type='Reverse') | |
for imgFilename in imgFilenames]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment