1

我正在尝试将图像保存到特定文件夹,但我不知道将路径放在哪里。savename在我的路径的尽头是一个变量。

相关代码:

for vv in object_pixel_value_list:
    hollowmask = Image.eval(mask,lambda i: findpixels(i, pixval=vv))
    blackmask = Image.eval(mask,makeblack)
    complete = Image.composite(blackmask,nomask,hollowmask)
    savename = 'complete.f%03d.png' % vv
    complete.save("~/Desktop/Experiment/Neuro_images/"savename.png, "png")

收到错误:

File "<ipython-input-52-f6a16ee0812c>", line 6
   complete.save("~/Desktop/Experiment/Neuro_images/"savename.png, "png")
                                                         ^
SyntaxError: invalid syntax
4

4 回答 4

3

您可能希望将 展开~到用户主目录。然后将保存名称添加到扩展路径。

import os
filepath = os.path.join(os.path.expanduser("~/Desktop/Experiment/Neuro_images/"), savename)
complete.save(filepath, "png")

在处理路径名时不要摆弄经典的字符串连接。

于 2013-02-18T21:00:03.220 回答
1

你要:

complete.save("~/Desktop/Experiment/Neuro_images/" + savename, "png")

于 2013-02-18T20:51:54.977 回答
1

complete.save("~/Desktop/Experiment/Neuro_images/%s" % savename) 希望有帮助...

于 2013-02-18T20:52:44.347 回答
1
complete.save("~/Desktop/Experiment/Neuro_images/"savename.png, "png")

应该

complete.save("~/Desktop/Experiment/Neuro_images/" + savename + ".png")
于 2013-02-18T20:53:58.197 回答