Home > Blockchain >  How can i add dynamic names to images screenshotted from pyautogui
How can i add dynamic names to images screenshotted from pyautogui

Time:11-07

I wants to save Screenshots taken by pyautogui.screenshot() but i want the files to have name like eg. Img1.png, Img2.png, Img3.png,.... and so on as more Screenshots are added.

But when i use img.save(r"D:\My Programs\Img.png"), I cant use

img.save(r`D:\My Programs\Img${ImgCount}.png`)

for it. any idea how can i do that?

Here is some code:

import pyautogui
import time

ImgCount = 1

while 1:
    img = pyautogui.screenshot()
    img.save(r"D:\My Programs\Img.png")
    print("SS saved")
    time.sleep(10)

CodePudding user response:

Your code isn’t doing anything with the variables.

Every 10 seconds you are just saving the same image.

What you want is to dynamically rename each image as you save them. Using r before the string won’t let you use the curly braces, you need to add f as well for that.

Let’s say you want 10 images every 10 seconds, you can do this.

import pyautogui
import time

ImgCount = 10
counter = 0

while counter < ImgCount:
    img = pyautogui.screenshot()
    img.save(rf"D:\My Programs\Img{counter}.png")
    print("SS saved")
    counter  = 1
    time.sleep(10)

Now you are incrementing the counter after each save and each image will be named img1.png, img2.png etc.

  • Related