0

I have a code that analysis images. The code analyzes only one image at a time and then shows the resulting analysis in the shell:

%matplotlib inline

from matplotlib import pyplot as plt 

FILEPATH = 'AnomalousImages/'
shortname = '3rd.png'
imagefile = FILEPATH+shortname
ggimg = cv2.imread(imagefile,0) 

maxintensity = np.amax(ggimg)
enhancefactor = 255./maxintensity
temp = enhancefactor*ggimg
hhimg = temp.astype(int)

print('Max intensity = ',maxintensity)
print('Enhancement factor = {0:.3f}'.format(enhancefactor))
print('Image dimensions',hhimg.shape)
plt.imshow(hhimg,cmap = 'gray')

plt.show()


brightpixels=np.nonzero(ggimg>THRESHOLD2-1)
numbright = len(brightpixels[0])
print('numbright = ',numbright)
if numbright > 0:
    for i in range(0,numbright):
        print('(',brightpixels[0][i],brightpixels[1][i],ggimg[brightpixels[0][i],brightpixels[1][i]],')',end='')

The problem is, there are over a hundred images inside the FILEPATH folder, and I need to do run this code for every picture in that folder. Instead of running this code one at a time for each image, I want to do them all at once but I'm not sure how.

I'm...pretty sure this is a very basic task, but I'm a very beginner coder. I'm pretty sure I have to use a for loop and have been messing around with the code trying to figure it out myself but to no avail.

Thanks in advance!

  • 1
    no sure but take a look at `os` module as there is a function that lists all files in a directory so you can use that and then just loop over each listed file – Matiiss Nov 12 '20 at 23:09
  • so [here](https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory) is a possible solution after getting that list you could just loop over each item in it – Matiiss Nov 12 '20 at 23:11

1 Answers1

1

You’ll want to make use of the glob module, this will allow you to get the path of every file in the folder as a list that you can then iterate over.

from glob import glob

image_list = glob(‘AnomalousImages/*’)

for image in image list:
...

https://docs.python.org/3/library/glob.html

Olaughter
  • 59
  • 3
  • Am I supposed to just run my code as originally written, except indented under the glob module you sent? Because I tried that and it isn't showing me any results. I thought it was because I'm using the Jupyter notebook and not an idle so I tried moving the print functions out from underneath the glob module and then it started giving me error messages. – Michael Belayneh Nov 13 '20 at 01:32