1
2

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.

[Python3] 画像サイズ一括変更

1
Last updated at Posted at 2019-09-08

ざっくりと。

Python3, OpenCVで画像サイズを一括変更する方法

今回は、画像の幅が2000pxより大きいときに2000px以下になるまで半分にしていきます。
縦横比は変わりません。

○○.jpgが一杯入っているフォルダに、以下のpyファイルをおき、実行します。

resize_images.py
import cv2
import glob

files = glob.glob("*.jpg")
for i, filename in enumerate(files):
    img = cv2.imread(filename)
    height = img.shape[0]
    width = img.shape[1]
    while width > 2000:
        width = int(width*0.5)
        height = int(height*0.5)
    img2 = cv2.resize(img, (width, height))
    cv2.imwrite(filename, img2)

print("resized.")

半分にするなどの処理を変更したい場合は、


    while width > 2000:
        width = int(width*0.5)
        height = int(height*0.5)

ここを好きなように書き換えてください。

ファイル名に全角文字が入っているとうまく読み込めないようです。注意してください。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?