简体   繁体   中英

Python Resize all images in a folder

I have the following code that I thought would resize the images in the specified path But when I run it, nothing works and yet python doesn't throw any error so I don't know what to do. Please advise. Thanks

import cv2
import numpy as np
import os
from PIL import Image
def image_resize(folder):
    images = []
    num_images = 0
    location = folder+"_"
    for filename in os.listdir(folder):
        img = cv2.imread(os.path.join(folder, filename))
        if img is not None:
            new_img = np.array(Image.fromarray(img).resize((200, 200), Image.ANTIALIAS))  # Resize the images to 50 X 50
            images.append(new_img)
            num_images += 1
            cv2.imwrite("{}/{}".format(location, filename), new_img)
            return None

image_resize("2S1")
image_resize("SLICY")
image_resize("BRDM-2")
image_resize("BTR-60")
image_resize("D7")
image_resize("T62")
image_resize("ZIL131")
image_resize("ZSU-23_4")

all image in folder: G:\\sar help me

It does not do anything because your for loop is not finding any files in the folder. You are never passing the "G:\\sar" part of the file path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM