0
1

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.

【Python】画像の比率を維持して長辺を指定した長さに

Last updated at Posted at 2019-07-11

コマンドで変更後の長辺の長さと画像を格納したディレクトリを指定します。
ファイル拡張子チェックは割愛。
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
0
1
0

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?