繁体   English   中英

使用枕头将 RBG 图像数据集转换为灰度图像的 Python 脚本

[英]Python script to convert RBG image dataset into Grayscale images using pillow

我想convert an image RGB dataset to Grayscale dataset using pillow 我想编写一个脚本,接收数据集路径并将所有图像一张一张地转换为灰度图。 最后我想保存这个脚本,并想在服务器上运行这个脚本以避免将大量数据复制到服务器。

可能这段代码对你有用吗?

代码:

import os
from PIL import Image
dir = '/content/pizza_steak/test/pizza'
for i in range(len(os.listdir(dir))):
    # directory where images are stored
    dir = '/content/pizza_steak/test/steak'

    # get the file name
    file_name = os.listdir(dir)[i]

    # creating a final path
    final_path = dir + '/' + file_name

    # convet and save the image
    Image.open(final_path).convert('L').save(f"/content/meow/gray{file_name}")

    # uncomment this is you want to delete the file
    # os.remove(final_path)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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