gpt4 book ai didi

python - 读取图像并有效调整其大小

转载 作者:行者123 更新时间:2023-12-02 16:19:21 25 4
gpt4 key购买 nike

我尝试读取一堆图像(超过180k),并将它们调整为新的高度和新的宽度。我使用下面的代码,它适用于少量的图像,但是对于大量的图像,内核就死了……那么,有没有更有效的方法来调整图像大小?也许不用阅读图像?

 folders = glob.glob(r'path\to\images\*')
imagenames_list = []
for folder in folders:
for f in glob.glob(folder+'/*.png'):
imagenames_list.append(f)
read_images = []

for image in imagenames_list:
read_images.append(cv2.imread(image))

def resize_images(img, new_width, new_height):
size = (new_width, new_height)
resized_img = cv2.resize(img, size)
return resized_img

resized_img = [resize_images(img, new_width=128, new_height=32) for img in read_images]

最佳答案

问题似乎是您正在将大约180,000张图像加载到read_images列表中。那是很多镜像,并且很可能会填满RAM并杀死内核。

由于您没有提到要对这些调整大小后的图像进行处理,因此我可以建议您做两件事。

  • 创建resize_and_save(image_path, new_size, save_path)以立即加载,保存和释放图像。您可以为imagenames_list
  • 中的每个图像使用此功能
  • (如果您使用它来创建用于训练的批次),则仅加载所需数量的图像以准备一个批次,并仅调整这些图像的大小

  • 关于python - 读取图像并有效调整其大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61900025/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com