1

I wrote the following code:

import os
import cv2
import random
from pathlib import Path

path = Path(__file__).parent
path = "../img_folder"
for f in path.iterdir():

    f = str(f)

    img=cv2.imread(f)

    im_height = img.shape[0]
    im_width = img.shape[1]

But when I run this code, I get the following error:

AttributeError: 'NoneType' object has no attribute 'shape' in im_height = img.shape[0].

I think I cannot access images, so I wrote print(img) and print(type(omg)) and None& is returned. img_folder is a folder that has 10 images. When I print out f, I get:

../img_folder/.DS_Store

I do not know why .DS_Store is contained because img_folder has only images. How should I fix this? What should I write this? Why can't I access images?

2
  • You would have to provide us more information. print f and post the output. And if the folder does not contain a lot of images, why not just use os.listdir ? Commented Dec 28, 2017 at 5:19
  • 1
    @GaneshTata, there's plenty of information: they're a macOS user, and do not know about hidden files (like .DS_Store), and how macOS stores .DS_Store files in every directory for settings by default. To the OP, you should filter only for images matching a glob pattern, for example, *.jpg. Commented Dec 28, 2017 at 5:22

3 Answers 3

5

You have post at least three questions about get filenames with "PostPath". Badly.

A better way is use glob.glob to get the specific type of filenames.

$ tree .
├── a.txt
├── feature.py
├── img01.jpg
├── img01.png
├── imgs
│   ├── img02.jpg
│   └── img02.png
├── tt01.py
├── tt02.py
└── utils.py

1 directory, 9 files

From current directory:

import glob
import itertools

def getFilenames(exts):
    fnames = [glob.glob(ext) for ext in exts]
    fnames = list(itertools.chain.from_iterable(fnames))
    return fnames


## get `.py` and `.txt` in current folder
exts = ["*.py","*.txt"]
res = getFilenames(exts)
print(res)
# ['utils.py', 'tt02.py', 'feature.py', 'tt01.py', 'a.txt']


# get `.png` in  current folder and subfolders
exts = ["*.png","*/*.png"]
res = getFilenames(exts)
print(res)
# ['img01.png', 'imgs/img02.png']
Sign up to request clarification or add additional context in comments.

2 Comments

thx ur answer.How should I get purpose file(image) by using if statement?
@user8817674 for example? what if statement do you want?
3

The .DS_Store file is a kind of hidden file, which is automatically generated (only on Mac OS), inside the various directories, which you may have opened using the Finder application. I guess it is some sort of cache file for fast and easy rendering of directory structure in the Finder. I have observed that it doesn't gets created if I do not open the directory with my Finder application.

To prevent this sort of errors, you must always check that the file you are going to read has a valid extension. It can be done as:

import os

for file_name in os.listdir("/path/to/your/directory"):
    if file_name.split(".")[-1].lower() in {"jpeg", "jpg", "png"}:
        img = cv2.imread("/path/to/your/directory/" + file_name)

Comments

1

It is because of a hidden file in your directory. If you are sure your directory contains only images, you can ignore hidden files/folders like below.

use

for f in path.iterdir():
    if not f.startswith('.'):

      f = str(f)

      img=cv2.imread(f)

      im_height = img.shape[0]
      im_width = img.shape[1]

4 Comments

A good solution in the future would be to use the glob module, rather than pathlib, to match only files with a desired extension. For example, glob.glob('*.jpg'), as this would work with directories containing extraneous data.
@Prakash thx ur answer.I wrote ur codes,but nothing can be gotten.It means the number of img is 0.What is wrong?
print all f before doing anything right after for loop. you will get some clue.
@PrakashPalnati f is ` ../img_folder/.DS_Store` & ../img_folder/01.png ・・・.I can understand why nothing is gotten.But how should I fix this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.