用python制作一个黑色和白色的文件夹

时间:2018-05-22 20:36:45

标签: python image scikit-image scikits

我有一个JPEG文件夹。它们都是RGB。我想用scikit-image将它们全部转换为灰度。

DOMException: Failed to set local answer sdp: Called in wrong state: kStable

我收到错误消息:

import glob
from skimage.color import rgb2gray
from skimage.io import imread, imsave
from skimage.filters import threshold_otsu
from skimage import img_as_uint
import os
from PIL import Image


list = os.chdir("C:/Users/Images/RGB")
for file in list:
    image1 = Image.open(file)
    image_converted = color.rgb2gray(image1)
    io.imsave("C:/Users/Images/Greyscale", image_converted)

我在这做错什么?

2 个答案:

答案 0 :(得分:2)

尝试使用此代码:

from skimage import io, color
import os
import imghdr

source = r'C:\Users\Images\RGB'
destination = r'C:\Users\Images\Greyscale'

image_files = [os.path.join(root, filename) 
                   for root, dirs, files in os.walk(source) 
                   for filename in files 
                   if imghdr.what(os.path.join(root, filename))]

for fn in image_files:
    rgb = io.imread(fn)
    grey = color.rgb2gray(rgb)
    head, tail = os.path.split(fn)
    io.imsave(os.path.join(destination, tail), grey)

请注意,image_files是一个列表,其中包含source及其子目录(递归)中找到的所有图像文件的完整路径名。可以在imghdr模块的文档中找到检测到的完整图像类型列表。

答案 1 :(得分:0)

看起来您没有在文件所在的目录中运行脚本。

Image.open(file)os.chdir的完整文件路径用于"C:/Users/Images/RGB"