1
4

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 3 years have passed since last update.

何百枚でも何千枚でもリサイズできます。

#縦横比を保たずリサイズ
anjyu3P2A6684_TP_V4.jpg   anjyu3P2A6684_TP_V4.jpg


import os
import glob
from PIL import Image

src = glob.glob('./dataset/*.jpg') # オリジナル画像のパスと拡張子を設定
dst = './dataset_resized/' # リサイズ画像の保存フォルダ

width = 513 # リサイズ後の横幅
height = 513 # リサイズ後の縦幅

for f in src:
    img = Image.open(f)
    img = img.resize((width,height))
    img.save(dst + os.path.basename(f))

#縦横比を保ってリサイズ
anjyu3P2A6684_TP_V4.jpg anjyu3P2A6684_TP_V4.jpg

横幅を指定する場合


import os
import glob
from PIL import Image

src = glob.glob('./dataset/*.jpg') # オリジナル画像のパスと拡張子を設定
dst = './dataset_resized/' # リサイズ画像の保存フォルダ

width = 513 # リサイズ後の横幅

for f in src:
    img = Image.open(f)
    original_width, original_height = img.size
    scale = width / original_width
    height = int(original_height * scale)
    img = img.resize((width,height))
    img.save(dst + os.path.basename(f))

縦幅を指定する場合


import os
import glob
from PIL import Image

src = glob.glob('./dataset/*.jpg') # オリジナル画像のパスと拡張子を設定
dst = './dataset_resized/' # リサイズ画像の保存フォルダ

height = 513 # リサイズ後の縦幅

for f in src:
    img = Image.open(f)
    original_width, original_height = img.size
    scale = height / original_height
    width = int(original_width * scale)
    img = img.resize((width,height))
    img.save(dst + os.path.basename(f))

🐣


お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
Medium

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?