gpt4 book ai didi

python - 如何在Python中将一个文件夹中的多个RGB图像转换为灰度图像

转载 作者:行者123 更新时间:2023-12-01 02:57:27 24 4
gpt4 key购买 nike

有人可以帮助我,我想将一个文件夹中的 RGB 图像一次性转换为灰度图像。我一直在寻找一些Python代码,但没有找到。我尝试按照以下方式进行操作,但没有成功。

这是我的代码:

from skimage.color import rgb2gray
from skimage.io import imread, imsave
from skimage.filters import threshold_otsu
from skimage import img_as_uint

inp_image = imread("C:/RGB/*.JPG")
img_gray = rgb2gray(inp_image)

thresh = threshold_otsu(img_gray)
binary_thresh_img = img_gray & gt; thresh

imsave("C:/Grayscale", img_as_uint(binary_thresh_img))

它给了我以下错误:

OSError: [Errno 22] Invalid argument: 'C:/RGB/*.JPG'

最佳答案

您可以使用 glob() 获取包含文件名的列表。

import glob
for filename in glob.glob("C:/RGB/*.JPG"):
inp_image = imread(filename)
[...]

关于python - 如何在Python中将一个文件夹中的多个RGB图像转换为灰度图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44083885/

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