2017-11-03 2 views
0
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 

에 쓰기 그레이 스케일로 나는 방법을 잘 모릅니다하지만 선택하기 및 변환되어있는 이미지는 그레이 스케일로 folder.Kindly변환 이미지는 파이썬과 OpenCV의를 사용하여 폴더의 모든 이미지를 선택하려고 특정 폴더

+0

** ImageMagick ** 명령 행에서 매우 간단하게 처리 할 수 ​​있습니다. 현재 디렉토리의 모든 JPEG를 그레이 스케일로 변환하고'greyscale'이라는 디렉토리에 씁니다 ...'mkdir greyscale; mogrify -path 그레이 스케일 -colorspace 회색 * .jpg' –

답변

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

이 코드는 경로에서 모든 이미지를 가지고 dstpath에 언급 된 다른 폴더로 작성합니다 도움이 특정에 그 이미지를 작성합니다.

관련 문제