- List item
I am a newbie to python. I wanted to know Instead of asking the user to give input file with a path, how can I automatically select input files one after another the from a folder. i.e, it should pick the first image file from the folder, do some processing, then pick the 2nd file, then 3rd file... and so on till all the files in that folder have been processed and do action when a condition from a function which is called is satisfied.?
I am trying compare_images:
def compare_images(img1, img2):
# normalize to compensate for exposure difference
img1 = to_grayscale(imread(img1).astype(float))
img2 = to_grayscale(imread(img2).astype(float))
img1 = normalize(img1)
img2 = normalize(img2)
# calculate the difference and its norms
diff = img1 - img2 # elementwise for scipy arrays
m_norm = sum(abs(diff)) # Manhattan norm
s = m_norm/img1.size
return s
This is where I'm calling the compare_images function.But this throws an error. It simply runs and stops without producing any output even on the console or throws error saying unable to find file even when it exists. I feel I'm going wrong in my approach. Help.
path=os.getcwd()
folder1 = os.listdir(path)
folder2 = os.path.join(path,"cd\\")
for filename1 in folder1:
for filename2 in os.listdir(folder2):
if filename1.endswith(".jpg"):
s = compare_images(filename1,filename2)
print(s)
if s > 10:
shutil.copy(filename1,folder2)
Please rectify me as to where I'm going wrong. How to copy files only when a condition is met and that condition is drawn from another function?