Image.save() - FileNotFoundError

时间:2017-03-29 17:39:42

标签: python-3.x tkinter screenshot python-imaging-library

我只是有一种工具栏。它允许您使用一个按钮截取屏幕截图:

def screenshot():
    try:
        os.mkdir(r"C:\Screenshots")
    except FileExistsError:
        pass

    global img
    img = ImageGrab.grab()
    global today
    today = time.strftime("%d_%B_%Y__%H_%m_%S")
    saveas=os.path.join(SaveDirectory, "Screenshot_" + today + ".jpg")
    img.save(saveas)
    bubble = Thread(target = balloon_tip, args = ("Saved!", "Screenshot was saved at C:\Screenshots"))
    bubble.start()

但是我收到此错误:

    Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python3.6\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Python3.6\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\mbilal25tr\Desktop\Python Projects\Toolbar py\toolbar.pyw", line 140, in screenshot
    img.save(saveas)
  File "C:\Python3.6\lib\site-packages\PIL\Image.py", line 1725, in save
    fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Screenshot\\Screenshot_29_March_2017__19_03_01.jpg'

但我不明白!是不是img.save()应该创建一个图像文件?为什么在创建之前尝试打开它?我怎么能指望它找到并运行没有错误?!

...谢谢

0 个答案:

没有答案