问题
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