簡體   English   中英

無法在python中保存PIL圖片

[英]Can't save PIL image in python

我正在嘗試從這里運行代碼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運行以下命令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完整路徑之前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。

對於解決此問題的任何建議,我將不勝感激!

如果您使用的是Windows 10,請確保關閉Ransomware Protection下的文件系統訪問設置。 該設置應位於Windows安全性 > 病毒和威脅防護 > 勒索軟件保護 >關閉受控文件系統訪問下

該設置禁止任何未經授權的程序或腳本更改系統上的文件。 從理論上講,以管理員身份運行腳本還應該解決當前的問題。 但是,關閉設置也會使您免於將來的麻煩。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM