CCZ23
CCZ23

Reputation: 151

Cannot iterate over a file?

I want to know how to apply a function over a file of images and save each of them in a separate file. For one image it works successfully, but i cannot apply it to all images.

import glob
images = glob.glob('/Desktop/Dataset/Images/*')

for img in images:
    img = np.array(Image.open(img))
    output = 'Desktop/Dataset/Output'
    MyFn(img = img,saveFile = output)

Upvotes: 1

Views: 125

Answers (3)

呂殿下-Luidenka
呂殿下-Luidenka

Reputation: 94

You did not define the sv value in your 2nd code snippet.

As the image will be overwrite, try this code:

import glob
images = glob.glob('/Desktop/Dataset/Images/*')

i = 0
for img in images:
    i += 1 #iteration to avoid overwrite
    img = np.array(Image.open(img))
    output = 'Desktop/Dataset/Output'
    MyFn(img = img + str(i),saveFile = output)

Upvotes: 3

olirwin
olirwin

Reputation: 605

This is because you are not setting the sv value in your loop. You should set it to a different value at each iteration in order for it to write to different files.

Upvotes: 0

Kal-1
Kal-1

Reputation: 199

try to use the library os directly with

import os
entries = os.listdir('image/') 

this will return a list of all the file into your folder

Upvotes: 1

Related Questions