file is not saved in particular directory using PIL im.save

对着背影说爱祢 提交于 2021-01-29 11:16:16

问题


I tried to resize some image files and save them in other directory. and there's not any error but files didn't save in directory I designated.

path = r"C:\Users\abc123\Desktop\various_image"
valid_images = [".jpg",".gif",".png",".tga"]

for f in os.listdir(path):
    ext = os.path.splitext(f)[1]
    if ext.lower() not in valid_images:
        continue
    im = Image.open(os.path.join(path,f))
    resize_im = im.resize((256, 256), Image.ANTIALIAS)
    ext = ".jpg"
    resize_im.save(r"C:\Users\abc123\Desktop\Movie" + f + ext, "JPEG" )
    break

I wanted to test 1 image before running the whole code so I put 'break'

what I wanted to test is 'f.jpg' is saved in C:\Users\abc123\Desktop\Movie as I said above there's not any error but file is not saved in directory. what's wrong with my code? thanks for your time.


回答1:


'+' does not actually add an element to the path, but concatenates with the string directly. So the program did in fact create a file, but not where you intended, but at "C:\Users\abc123\Desktop\MovieFile.jpg", assuming that there is file called File in the "C:\Users\abc123\Desktop\various_image" folder.

I think what you were trying to write was

resize_im.save(os.path.join(r"C:\Users\abc123\Desktop\Movie", f + ext), "JPEG" )


来源:https://stackoverflow.com/questions/57131453/file-is-not-saved-in-particular-directory-using-pil-im-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!