【问题标题】:Can't save PIL image in python无法在python中保存PIL图像
【发布时间】:2019-07-14 14:42:28
【问题描述】:

我正在尝试从这里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'

但是当我在jupiter-notebook 中启动这个脚本时,它可以正常工作。

我使用:Windows 10、Python 3.7.1、Pillow 5.3.0。

如果您有任何解决此问题的建议,我将不胜感激!

【问题讨论】:

  • 你能尝试给一个固定的路径吗?喜欢:C:\Users\Fabian\Desktop\image.png
  • 当我将您的代码复制粘贴到我的 Windows 10 中时,它按预期工作。枕头 5.4.1 和 Python 3.5.0。你也可以试试with open(filepath, 'w') as f:img.save(f)

标签: python image-processing python-imaging-library pytorch


【解决方案1】:

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

该设置禁止任何未经授权的程序或脚本更改系统上的文件。理论上,以管理员身份运行脚本也应该可以解决眼前的问题。但是,关闭该设置也可以让您免于日后的麻烦。

【讨论】:

    猜你喜欢
    • 2016-04-11
    • 1970-01-01
    • 2022-01-14
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2011-09-21
    相关资源
    最近更新 更多