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



我想要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)

最新更新