コマンドで変更後の長辺の長さと画像を格納したディレクトリを指定します。
ファイル拡張子チェックは割愛。
python2.7で動作確認してますが、print関数だけ直せば3でも動くでしょう。
コード
resize_max_side.py
# !/usr/bin/python
# -*- coding: UTF-8 -*-
"""feature detection."""
import sys
import os
import cv2
def resize(image, max_side_size):
ori_height, ori_width = image.shape[:2]
modi_height = max_side_size
modi_width = max_side_size
if ori_height > ori_width:
modi_width = ori_width * modi_height / ori_height
else:
modi_height = ori_height * modi_width / ori_width
size = (modi_height, modi_width)
print str(size)
resized_image = cv2.resize(image, size)
return resized_image
if __name__ == '__main__':
args = sys.argv
dir_path = args[1]
max_side_size = int(args[2])
file_names = os.listdir(dir_path)
for file_name in file_names:
file_path = dir_path + file_name
image = cv2.imread(file_path)
resized_image = resize(image, max_side_size)
cv2.imwrite(file_path, resized_image)
実行
# python resize_max_side.py images/ 100