user3565613
user3565613

Reputation: 1

How can i load an image with the same name number of for cycle iteration on python?

I have a question I want to load a few images from a folder into an array . These images are named ( 1.jpg , 2.jpg , 3.jpg ) and want to load corresponding image to the number of the iteration for cycle .

E.i

If this cycle for at iteration 2 I should load 2.jpg image .

Thank you

pictures = []

i = 0

for i in range (0,5):
    pictures[i] = load_image ('images/1.jpg') # 1.jpg should be i.jpg

As I do ??

Thank

Upvotes: 0

Views: 121

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180441

use str.format passing in i through each iteration

for i in range(5):
    pictures[i] = load_image('images/{}.jpg'.format(i))


In [1]: for i in range(5):
   ...:     print ('images/{}.jpg'.format(i))
   ...:     
images/0.jpg
images/1.jpg
images/2.jpg
images/3.jpg
images/4.jpg

If you want start at 1 use range(1,5)

Upvotes: 1

Related Questions