skg22
skg22

Reputation: 37

Python - IO:Error Cannot Identify Image File

I'm working on an image to text OCR system and I kept receiving an error on the console stating

Traceback (most recent call last):
  File "crack_test.py", line 48, in <module>
    temp.append(buildvector(Image.open("./iconset/%s/%s"%(letter,img))))
  File "/Users/seng_kin/anaconda/lib/python2.7/site-packages/PIL/Image.py", line 2290, in open
    % (filename if filename else fp))
IOError: cannot identify image file './characterset/a/.DS_Store'

I have a folder in a ../OCR/characterset/ directory which has 26 subfolders representing 26 characters whereby each subfolder contains a .PNG of a character.

The score_test.py is stored in ../OCR/

score_test.py Code:

from PIL import Image
import os, sys
import math

****def functions****

iconset = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
           's','t','u','v','w','x','y','z']

imageset = []

for letter in iconset:
    for img in os.listdir('./characterset/%s/'%(letter)):
        temp = []

        if img != "Thumbs.db": #windows check...
            temp.append(buildvector(Image.open("./characterset/%s/%s"%(letter, img))))

        imageset.append({letter:temp})

I saw other solutions on the website but all is related to the absence of from PIL import Image. Am I missing something?

Upvotes: 1

Views: 2241

Answers (1)

dursk
dursk

Reputation: 4445

.DS_Store is a "hidden" file in directories on Mac OS X.

In your if img != "Thumbs.db" check, you should add:

if not img.startswith('.') and img != 'Thumbs.db':

this way you filter out all "hidden" files.

Upvotes: 4

Related Questions