简体   繁体   中英

Just rotate image

what is my mistake?

I just want to rotate original image and save him to output folder.

from pathlib import Path
from PIL import Image, ImageFilter

for image in Path('/home/lol/Pictures').glob('**/*.png'):
    img = Image.open(image)
    img = img.rotate(180)
    img.save('/home/lol/Pictures/output/{}'.format(image.name))

    print('Done: {}'.format(image))

Console output:

Done: /home/lol/Pictures/image.png
Done: /home/lol/Pictures/output/image.png

And the picture in output is the same like original.

It looks like the script is successfully rotating Pictures/image.png and saving it in your output directory. But it doesn't stop there: glob iterates its way into the output folder, and rotates all of those pictures too. So your original output/image.png gets overwritten with a copy of output/image.png , except rotated 180 degrees. In other words, it's your original image, rotated 360 degrees - an identical copy.

You should make sure that glob doesn't iterate over the output directory. You could move output to somewhere other than home/lol/Pictures . Or perhaps you could manually skip over the output directory, with something like if "/output/" in image: continue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM