[Python] Problem Scanning Number of Images in Folder

apex1

Regular Member
Joined
May 29, 2015
Messages
217
Reaction score
182
Hey guys, here's my code so far:
Code:
for img_scan in os.listdir('./test_folder'):
    if img_scan.endswith('.jpg'):
        print(str(img_scan))

It will output a list of image names from within the folder, example:

img1.jpg
img2.jpg
img3.jpg

But I can't get the damn thing to just tell me there are "3" images.

I tried using sum() and some other things but no luck.

Also, for some reason I can't add more image types on this line:
Code:
if img_scan.endswith('.jpg', '.jpeg', '.png', '.gif'):

^This throws an error.

Any ideas how to fix it?
 

nikhil94

Regular Member
Joined
Apr 17, 2012
Messages
338
Reaction score
92
Maybe add a counter just before the print statement to count the number of jpg files?
Code:
c=0
for img_scan in os.listdir('./test_folder'):
    if img_scan.endswith('.jpg'):
        c+=1
        print(str(img_scan))
print c

And to add multiple extensions use OR

Code:
if img_scan.endswith('.jpg') or img_scan.endswith('.jpeg'):
 

bk071

Elite Member
Joined
Nov 24, 2010
Messages
3,348
Reaction score
8,830
I was going to post what @nikhil94 did above. Just use a counter, initialize it at 0 outside the loop and increment it for every img_scan.
 

bigot

Registered Member
Joined
May 9, 2017
Messages
76
Reaction score
39
Also, for some reason I can't add more image types on this line:
Code:
if img_scan.endswith('.jpg', '.jpeg', '.png', '.gif'):
^This throws an error.

endswith doesn't work like that; https://docs.python.org/3/library/stdtypes.html#str.endswith

First parameter (mandatory) is what it finds.
Second parameter (optional) is where it starts searching.
Third parameter (optional) is where it stops searching.

You'll have to chain multiple endswiths with or as stated above by @nikhil94.
 

YesAndNo

BANNED
Joined
Nov 20, 2017
Messages
25
Reaction score
37
Age
22
So what you generally want to do is make 2 loops, one to go over every single image in the folder, and another one to check for every type of image type. Also, you should learn in the future once you get better PEP-8 to code appropriately for python.

Code:
for i, img_scan in enumerate(os.listdir('./test_folder')):
    for img_type in ('.jpg', '.jpeg', '.png', '.gif'):
        if img_scan.endswith(img_type):
            print(str(img_scan))

When you call enumerate() on a iterable, you will get a tuple back which is the index, and the value. So i represents the index, which is a replacement for a counter as previously stated. And img_scan will equal your value, just like on your older loop.
 

bigot

Registered Member
Joined
May 9, 2017
Messages
76
Reaction score
39
Code:
for i, img_scan in enumerate(os.listdir('./test_folder')):
    for img_type in ('.jpg', '.jpeg', '.png', '.gif'):
        if img_scan.endswith(img_type):
            print(str(img_scan))

This works but is a little inefficient; when it finds a "jpg", it will keep checking if it's "jpeg", "png" or "gif". Whatever it finds, it will check subsequent extensions regardless.

If you chain ORs, most languages won't bother evaluating subsequent conditions after the first TRUE result. Simliarly, these languages won't bother evaluating subsequent conditions after the first FALSE in chained AND conditions. Edit: Not sure if Python does this.

If you use the code above, I suggest you add break after print. This will make it skip the subsequent img_types.
 

YesAndNo

BANNED
Joined
Nov 20, 2017
Messages
25
Reaction score
37
Age
22
This works but is a little inefficient; when it finds a "jpg", it will keep checking if it's "jpeg", "png" or "gif". Whatever it finds, it will check subsequent extensions regardless.

If you chain ORs, most languages won't bother evaluating subsequent conditions after the first TRUE result. Simliarly, these languages won't bother evaluating subsequent conditions after the first FALSE in chained AND conditions. Edit: Not sure if Python does this.

If you use the code above, I suggest you add break after print. This will make it skip the subsequent img_types.

This times 10.

Good catch @bigot, forgot to do the break to avoid checking for the other image formats. My loop is a lot slower due to the unnecessary checking.

Also, @bigot advice is spot on. If you chain the or in your if statement, the evaluation will stop evaluating the other or statements as soon one becomes true.

@bigot thanks for the correction.
 
Top