Reputation: 1
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
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