# Revision history [back]

### Can't iterate-open images

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"):
print image
if (image == None):
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

print image
if (image == None):

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


### Can't iterate-open images

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"):
print image
if (image == None):
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

print image
if (image == None):

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


### Can't iterate-open imagesimages (SOLVED)

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"):
print image
if (image == None):
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

print image
if (image == None):

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


EDIT: SOLVED

By changing

image = imread(img)


to

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