使用python和opencv将文件夹中的图像转换为灰度并将其写入特定文件夹

使用python和opencv将文件夹中的图像转换为灰度并将其写入特定文件夹,python,opencv,grayscale,Python,Opencv,Grayscale,尝试从文件夹中选择所有图像,图像被选中并转换为灰度,尽管我不知道如何将这些图像写入特定文件夹。请帮助 import glob import cv2 import os import numpy as np from PIL import Image images=[] images=np.array(images) path='C:\Users\Quantum\Desktop\test' count=0 images = [cv2.imread(file,0) for file in glob.

尝试从文件夹中选择所有图像,图像被选中并转换为灰度,尽管我不知道如何将这些图像写入特定文件夹。请帮助

import glob
import cv2
import os
import numpy as np
from PIL import Image
images=[]
images=np.array(images)
path='C:\Users\Quantum\Desktop\test'
count=0
images = [cv2.imread(file,0) for file in glob.glob("E:\homework\Computer vision\Faces\*.jpg")]
for i in range(len(images)):
#    im = Image.fromarray(images[i])
#    cv2.imwrite(str(path) + '.jpg', images[count])
    cv2.imwrite(os.path.join(path, 'pic.jpg'), images[count])
    count+=1
此代码段将从path获取所有图像,并写入dstpath中提到的另一个文件夹

此代码段将从路径获取所有图像,并写入dstpath中提到的另一个文件夹

#多个图像转换

import cv2
from os import listdir,makedirs
from os.path import isfile,join

path = r'C:\Users\fakabbir.amin\Desktop\pdfop' # Source Folder
dstpath = r'C:\Users\fakabbir.amin\Desktop\testfolder' # Destination Folder

try:
    makedirs(dstpath)
except:
    print ("Directory already exist, images will be written in asme folder")

# Folder won't used
files = [f for f in listdir(path) if isfile(join(path,f))] 

for image in files:
    try:
        img = cv2.imread(os.path.join(path,image))
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        dstPath = join(dstpath,image)
        cv2.imwrite(dstPath,gray)
    except:
        print ("{} is not converted".format(image))
#多重图像转换

import cv2
from os import listdir,makedirs
from os.path import isfile,join

path = r'C:\Users\fakabbir.amin\Desktop\pdfop' # Source Folder
dstpath = r'C:\Users\fakabbir.amin\Desktop\testfolder' # Destination Folder

try:
    makedirs(dstpath)
except:
    print ("Directory already exist, images will be written in asme folder")

# Folder won't used
files = [f for f in listdir(path) if isfile(join(path,f))] 

for image in files:
    try:
        img = cv2.imread(os.path.join(path,image))
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        dstPath = join(dstpath,image)
        cv2.imwrite(dstPath,gray)
    except:
        print ("{} is not converted".format(image))

顺便说一下,您可以使用ImageMagick在命令行中非常简单地执行此操作。将当前目录中的所有JPEG转换为灰度,并写入名为
greyscale
<代码>mkdir灰度;mogrify-path greyscale-colorspace gray*.jpg顺便说一句,您可以使用ImageMagick在命令行中非常简单地执行此操作。将当前目录中的所有JPEG转换为灰度,并写入名为
greyscale
<代码>mkdir灰度;mogrify-path greyscale-colorspace gray*.jpg虽然这个代码片段可以解决这个问题,但它确实有助于提高您的文章质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。虽然此代码片段可以解决问题,但确实有助于提高您文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
import cv2

import os,glob

from os import listdir,makedirs

from os.path import isfile,join
path = '/root/Desktop/Anil' # Source Folder
dstpath = '/root/Desktop/Anil2' # Destination Folder
try:
    makedirs(dstpath)
except:
    print ("Directory already exist, images will be written in same folder")
# Folder won't used
files = list(filter(lambda f: isfile(join(path,f)), listdir(path)))
for image in files:
    try:
        img = cv2.imread(os.path.join(path,image))
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        dstPath = join(dstpath,image)
        cv2.imwrite(dstPath,gray)
    except:
        print ("{} is not converted".format(image))
for fil in glob.glob("*.jpg"):
    try:
        image = cv2.imread(fil) 
        gray_image = cv2.cvtColor(os.path.join(path,image), cv2.COLOR_BGR2GRAY) # convert to greyscale
        cv2.imwrite(os.path.join(dstpath,fil),gray_image)
    except:
        print('{} is not converted')
        
import os,cv2
path = r'C:\Users\me\Desktop\folder' # Source Folder
dstpath = r'C:\Users\me\Desktop\desfolder' # Destination Folder



try:
    makedirs(dstpath)
except:
    print ("Directory already exist, images will be written in asme folder")

# Folder won't used
files = os.listdir(path)

for image in files:
    img = cv2.imread(os.path.join(path,image))
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    cv2.imwrite(os.path.join(dstpath,image),gray)