保存多张图片并分别命名?
Saving multiple images and naming them indvidually?
我正在尝试截取一个区域的屏幕截图,在一个循环中,将屏幕截图保存到一个文件夹并重命名它们,这样它们就不会相互覆盖。
我想要一个文件夹,里面装满了具有唯一名称的图像,最好是时间戳,但我还没有弄清楚,所以我现在使用计数器。
import pyautogui as py
for image in image_list:
screenshot = py.screenshot(region=(x,y,w,h)
save_path = r'C:\'
count = count + 1
name_of_file = count
completeName = os.path.join(save_path, name_of_file+".png")
screenshot.save(completeName)
给我这个错误;
Exception has occurred: TypeError
unsupported operand type(s) for +: 'int' and 'str'
completeName = os.path.join(save_path, name_of_file+".png")
我试过:
- 用
str
包装 name_of_file+
:completeName = os.path.join(save_path, (str(name_of_file+".png")))
但是我得到了同样的错误信息。
我试过了:
- 将
+
换成 ,
- 包装一切
completeName = os.path.join(str(save_path), (str(name_of_file,".png")))
那些给了我不同的错误信息:TypeError: decoding to str: need a bytes-like object, int found
错误表明您的变量不是浮点数,因此:
completeName = os.path.join(save_path, name_of_file+".png")
应改为:
completeName = os.path.join(save_path, str(name_of_file)+".png")
考虑使用 pathlib 在脚本中构建路径。这是一种比使用 r"C:\"
.
更可靠的方法
你可以试试 file_name_<>.png 像-
(此格式适用于 cv2.write。希望也适用于 pyautogui)
count = 0
completeName = "<save_path>/screenshot_%d.png" %count
count = count + 1
screenshot.save(completename)
我正在尝试截取一个区域的屏幕截图,在一个循环中,将屏幕截图保存到一个文件夹并重命名它们,这样它们就不会相互覆盖。
我想要一个文件夹,里面装满了具有唯一名称的图像,最好是时间戳,但我还没有弄清楚,所以我现在使用计数器。
import pyautogui as py
for image in image_list:
screenshot = py.screenshot(region=(x,y,w,h)
save_path = r'C:\'
count = count + 1
name_of_file = count
completeName = os.path.join(save_path, name_of_file+".png")
screenshot.save(completeName)
给我这个错误;
Exception has occurred: TypeError
unsupported operand type(s) for +: 'int' and 'str'
completeName = os.path.join(save_path, name_of_file+".png")
我试过:
- 用
str
包装name_of_file+
:completeName = os.path.join(save_path, (str(name_of_file+".png")))
但是我得到了同样的错误信息。
我试过了:
- 将
+
换成,
- 包装一切
completeName = os.path.join(str(save_path), (str(name_of_file,".png")))
那些给了我不同的错误信息:TypeError: decoding to str: need a bytes-like object, int found
错误表明您的变量不是浮点数,因此:
completeName = os.path.join(save_path, name_of_file+".png")
应改为:
completeName = os.path.join(save_path, str(name_of_file)+".png")
考虑使用 pathlib 在脚本中构建路径。这是一种比使用 r"C:\"
.
你可以试试 file_name_<>.png 像- (此格式适用于 cv2.write。希望也适用于 pyautogui)
count = 0
completeName = "<save_path>/screenshot_%d.png" %count
count = count + 1
screenshot.save(completename)