Ali Fateh
Ali Fateh

Reputation: 1

Multiple image names

I need to save images after cropping that image in a for loop, how can this be achieved?

I have tried using img2.save("img"+i+".png") but this gives an error.

for file in files(path):
            if file.endswith('.png'):
                img=Image.open(file)
                img2 = img.crop((x0,y0,x1,y1))
                img2.save("img"+i+".png")
                i+=1

The output should be as follows: 1. image1_crop.png 2. image2_crop.png ....

Upvotes: 0

Views: 55

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

You forgot to post the exact error message but you obviously have a TypeError here:

img2.save("img"+i+".png")

since adding strings and numbers is not allowed (for the obvious reason that it makes no sense at all).

You want to use string formating instead:

            img2.save("img{}.png".format(i))

Upvotes: 2

Related Questions