【问题标题】:Converting images in a folder to grayscale using python and opencv and writing it to a specific folder使用python和opencv将文件夹中的图像转换为灰度并将其写入特定文件夹
【发布时间】:2018-04-15 16:46:41
【问题描述】:
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

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

【问题讨论】:

  • 顺便说一句,您可以在命令行中使用 ImageMagick 非常简单地完成此操作。将当前目录中的所有 JPEG 转换为灰度并写入名为 greyscale... mkdir greyscale; mogrify -path greyscale -colorspace gray *.jpg 的目录

标签: python opencv grayscale


【解决方案1】:

#多个图像转换

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')
        

【讨论】:

    【解决方案2】:
    import cv2
    import glob, os, errno
    
    # Replace mydir with the directory you want
    mydir = r'C:\Users\Quantum\Desktop\testoutput'
    
    #check if directory exist, if not create it
    try:
        os.makedirs(mydir)
    except OSError as e:
        if e.errno == errno.EEXIST:
            raise
    for fil in glob.glob("*.jpg"):
        image = cv2.imread(fil) 
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # convert to greyscale
        cv2.imwrite(os.path.join(mydir,fil),gray_image) # write to location with same name
    

    【讨论】:

      【解决方案3】:
      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))
      

      这段代码 sn-p 将从 path 获取所有图像并写入 dstpath 中提到的另一个文件夹。

      【讨论】:

        【解决方案4】:
        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)
        

        【讨论】:

        • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-24
        • 2023-01-02
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-21
        相关资源
        最近更新 更多