无法在Python中保存PIL图像

3

我试图运行来自这里的代码https://github.com/pytorch/examples/tree/master/fast_neural_style,我从终端运行以下命令python neural_style/neural_style.py eval --content-image ./images/content-images/amber.jpg --model ./saved_models/mosaic.pth --output-image a.jpg --cuda 0,其中a.jpg是输出图像,但是每次我都会收到一个错误:

Traceback (most recent call last):
  File "neural_style/neural_style.py", line 240, in <module>
    main()
  File "neural_style/neural_style.py", line 236, in main
    stylize(args)
  File "neural_style/neural_style.py", line 150, in stylize
    utils.save_image(args.output_image, output[0])
  File "C:\Users\Lenovo\Documents\fast_neural_style\neural_style\utils.py", line 19, in save_image
    img.save(filename)
  File "C:\Users\Lenovo\Anaconda3\lib\site-packages\PIL\Image.py", line 1991, in save
    fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: 'a.jpg'

我之前尝试使用斜杠和反斜杠来指定a.jpg的完整路径,但每次仍然出现此错误。

我从终端启动了以下脚本并使用命令python test.py

from PIL import Image, ImageDraw
text = "test image"
color = (0, 0, 120)
img = Image.new('RGB', (100, 50), color)
imgDrawer = ImageDraw.Draw(img)
imgDrawer.text((10, 20), text)
img.save("pil-example.png")

又出现了一个错误

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    img.save("pil-example.png")
  File "C:\Users\Lenovo\Anaconda3\lib\site-packages\PIL\Image.py", line 1966, in save
    fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: 'pil-example.png'

但是当我在Jupyter Notebook中运行这个脚本时,它没有出现错误。

我使用的是:Windows 10操作系统,Python 3.7.1版本,Pillow 5.3.0库。

如果您能给我解决这个问题的任何建议,我将非常感激!


1
你能否尝试给出一个固定的路径?例如:C:\Users\Fabian\Desktop\image.png - Fabian
当我将您的代码复制粘贴到我的Windows 10中时,它按预期工作。使用Pillow 5.4.1和Python 3.5.0。您也可以尝试以下代码:with open(filepath, 'w') as f: img.save(f) - Reedinationer
1个回答

0

如果您使用的是Windows 10,请确保关闭勒索软件保护下的文件系统访问设置。该设置应位于Windows安全 > 病毒和威胁防护 > 勒索软件保护 > 关闭受控文件系统访问

该设置禁止任何未经授权的程序或脚本更改您系统上的文件。理论上,以管理员身份运行脚本也可以解决即时问题。但是,关闭该设置还可以避免将来的麻烦。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接