Can't iterate-open images (SOLVED) [closed]

asked 2018-03-02 09:39:41 -0500

AcrimoniousMirth gravatar image

updated 2018-03-02 10:39:43 -0500

Hi all, this should be an easy fix (forgive my n00bness, I'm a student and coding is not my forte).

So as part of my project here I'm needing to iterate through a directory and open each image to operate on. Each image is named as ximgy.png where x and y are extracted values used to determine how the image is handled.

Now, I've been struggling with this for a short while, the functions I wrote prior to attempting iteration worked fine but my iteration code (extracted from script) doesn't work. My first check of print image shows the image being read in is None.

def imagesToList():
# Iterates through directory and processes each image into array

    for img in os.listdir(DIRECTORY + 'TestPhotos/'):
        # Filters out any rogue files (e.g. hidden)
        if img.endswith(".png"):
            image = cv.imread(img)
            print image
            if (image == None): 
                print(DIRECTORY+'TestPhotos/'+img, "could not be read!")
                return # can't go on with an invalid image

            camNum, imgNum = os.path.splitext(img)[0].split('img')
            # print 'Camera is ', camNum
            # print 'Image is ', imgNum

            # Correction of values will be different for cams 1 & 2
            if camNum == 1:
                # Correction values here

            zVal = imgNum*numMoves
            #imageToPoints(img)
            print img
            cv.imshow('image',image)
            cv.waitKey()

I also set up a simple test and tried writing 2img1.png which worked. I'm not sure exactly what's going wrong with the iterator read. import cv2 as cv

DIRECTORY = '/home/pi/Desktop/ScannerDev/TestPhotos/'
imgName = raw_input('Enter image name: ')
img = DIRECTORY + imgName


image = cv.imread(img)
print image
if (image == None): 
   print(DIRECTORY+'TestPhotos/'+img, "could not be read!")

cv.imshow('image',image)
cv.waitKey()

EDIT: SOLVED


By changing

image = imread(img)

to

image = imread(DIRECTORY + 'TestPhotos/' + img) It now works.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by AcrimoniousMirth
close date 2018-03-02 12:41:50.122877