文件未使用 PIL im.save 保存在特定目录中 [英] file is not saved in particular directory using PIL im.save

查看:87
本文介绍了文件未使用 PIL im.save 保存在特定目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试调整一些图像文件的大小并将它们保存在其他目录中.没有任何错误,但文件没有保存在我指定的目录中.

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

我想在运行整个代码之前测试 1 个图像,所以我输入了 'break'

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

我想测试的是 'f.jpg' 保存在 C:\Users\abc123\Desktop\Movie正如我上面所说,没有任何错误,但文件未保存在目录中.我的代码有什么问题?感谢您抽出宝贵时间.

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.

推荐答案

'+' 实际上并不向路径添加元素,而是直接与字符串连接.所以程序确实创建了一个文件,但不是在你想要的地方,而是在 "C:\Users\abc123\Desktop\MovieFile.jpg",假设有一个名为 File 的文件"C:\Users\abc123\Desktop\various_image" 文件夹中.

'+' 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.

我认为你想写的是

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

这篇关于文件未使用 PIL im.save 保存在特定目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆