我正在编码一个循环,该循环将截取屏幕截图,并以与先前相同的名称保存图像。问题在于,在随机迭代之后,程序将中止,并出现以下错误:
C:\Folder>python test.py
Traceback (most recent call last):
File "test.py", line 30, in <module>
screen_record()
File "test.py", line 19, in screen_record
printscreen.save(img_name)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2004, in save
fp = builtins.open(filename, "w+b")
IOError: [Errno 13] Permission denied: 'screen.jpg'
可能是什么问题?感谢您的支持
代码:
import numpy as np
from PIL import ImageGrab
import cv2
import time
import os
from matplotlib import pyplot as plt
from pynput.keyboard import Key, Controller
def screen_record():
keyboard = Controller()
img_name = 'screen.jpg'
while(True):
printscreen = ImageGrab.grab(bbox=(0,0,385,697))
printscreen.save(img_name)
img_rgba = cv2.imread(img_name,cv2.IMREAD_UNCHANGED)
cv2.namedWindow('window', cv2.WINDOW_NORMAL)
cv2.imshow('window', img_rgba)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
screen_record()
答案 0 :(得分:1)
问题解决了。最终代码:
import numpy as np
from PIL import ImageGrab
import cv2
import time
import os
from matplotlib import pyplot as plt
from pynput.keyboard import Key, Controller
def screen_record():
keyboard = Controller()
cv2.namedWindow('window', cv2.WINDOW_NORMAL)
while(True):
printscreen = np.array(ImageGrab.grab(bbox=(0, 0, 385, 697)))
RGB_img = cv2.cvtColor(printscreen, cv2.COLOR_BGR2RGB)
cv2.imshow('window', RGB_img)
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
screen_record()