python读取文件夹下所有图片的灰度值_使用python和opencv将文件夹中的图像转换为灰度并将其写入特定的文件夹...

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

Trying to select all the images from a folder and the images are getting selected and are converted to grayscale although I dont know how to write those images to a specific folder.Kindly help

解决方案

#multiple image conversions

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 = [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))

for fil in glob.glob("*.jpg"):

try:

image = cv2.imread(fil)

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to greyscale

cv2.imwrite(os.path.join(dstpath,fil),gray_image)

except:

print('{} is not converted')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值