2017-10-03 1 views
0

20 개의 그림이있는 폴더가 있습니다. 폴더를 열고 모든 그림을 스캔하고 모든 그림에 대해 고유 한 행렬을 만들고 싶습니다.Python - 그림이있는 폴더 열기

어떻게하면됩니까?

from os import listdir 
from PIL import Image as PImage 

def loadImages(path): 
    # return array of images 

    imagesList = listdir(path) 
    loadedImages = [] 
    for image in imagesList: 
     img = PImage.open(path + image) 
     loadedImages.append(img) 

    return loadedImages 

path = open(r"C:\Users\yasmin\Desktop\weeds\type1",encoding='utf-8') 

# your images in an array 
imgs = loadImages(path) 

for img in imgs: 
    # you can show every image 
    img.show() 
+0

https://docs.python.org/2/library/os.html https://pillow.readthedocs.io/en/4.3 .x/ –

+0

아무 것도 시도해 보셨습니까? Google에 시도 했습니까? 몇 가지 코드를 공유하십시오. – planet260

+0

@ planet260 수정됨 – Yasmin

답변

0

이미지 처리를 위해 내가 PIL을 제안 : documentation

폴더 및 파일 처리를위한 운영 체제 라이브러리를보십시오.

예 :

import os 
from PIL import Image 

for file in os.listdir(path): 
    image = Image.open(os.path.abspath(path + "/" + file)) 
    pixels = image.load() 

편집 :

내가 loadImages에서 다음과 같이 변경합니다 :

def loadImages(path): 
    imagesList = listdir(path) 
    loadedImages = [] 
    for image in imagesList: 
     if image[-3:] in ["png", "jpg"]: 
      img = PImage.open(path + "/" + image) 
      loadedImages.append(img) 

    return loadedImages 

을 당신은 폴더와 디렉토리 분리를 ("/") 누락 및 파일 이름. 파일의 확장자가 jpg 또는 png인지 확인합니다.

+0

죄송합니다. 모든 사진을 i로 여는 방법을 이해하지 못합니다. 파이썬에서 새로운 litlle을 원한다. – Yasmin

0

첫 번째 문제는 encoding= 끝에 '이 누락되었습니다. 그러나 그 라인은 전혀 필요 없습니다. os.listdir()에는 이미지가 포함 된 폴더의 경로 인 문자열이 필요합니다. open 두 번째 이유는 "권한 거부"오류입니다. 따라서, 단순히 대체 :

path = open(r"C:\Users\yasmin\Desktop\weeds\type1",encoding='utf-8') 

로 :

path = "C:\Users\yasmin\Desktop\weeds\type1"