1

I wrote a Python code to take screenshots at random intervals and now I'm trying to save my screenshots in a list so I'll be able to print them out

Here's my code

def takeScreenshot ():
    count=0
    name = random.randrange(1, 1000)
    full_name = str(name) + ".jpg"
    myScreenshot = pyautogui.screenshot()
    myScreenshot.interval = 100
# generate a random time between 120 and 300 sec
    random_time = random.randrange(20, 30)
# wait between 120 and 300 seconds (or between 2 and 5 minutes)
    time.sleep(random_time)
    myScreenshot.save((r'C:\Users\ZainbaMuhdYunus\Desktop\shots2\shot') + full_name)
    # keeps doing count
while 1:
     takeScreenshot(data)

this is the reviewed code for the question i've re viewed it , maybe it's self explanatory now

import random
import pyautogui
import tkinter as tk
from tkinter import filedialog

root= tk.Tk()
def takeScreenshot ():
    # count=0
    name=random.randrange(1,1000)
    full_name=str(name) + ".jpg"
    myScreenshot = pyautogui.screenshot()
    myScreenshot.interval = 100
# generate a random time between 120 and 300 sec
    random_time = random.randrange(2,10)
# wait between 120 and 300 seconds (or between 2 and 5 minutes)
    time.sleep(random_time)
    myScreenshot.save((r'C:\Users\ZainbaMuhdYunus\Desktop\shots2\shot') + full_name)

    # keeps doing count

counter=0
while 1:
    takeScreenshot()```
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
fyunusa
  • 95
  • 1
  • 8
  • 2
    Does this answer your question? [Pyautogui screenshot. Where does it go? How to save and find later?](https://stackoverflow.com/questions/50730539/pyautogui-screenshot-where-does-it-go-how-to-save-and-find-later) – ferhen Feb 12 '20 at 14:36
  • 1
    Just return the string you pass to save and append to a list in your while loop... – kpie Feb 12 '20 at 14:39
  • It will be helpful if you add details to what exactly you are trying to achieve and what is actually wrong right now. Because from looking at your code it seems to be pretty fine. Read [ask] and be more clear and detailed about your problem. BTW @ferhen I believe this dup is not relevant as OP already does what is stated there. The problem here is a bit different – Tomerikoo Feb 12 '20 at 14:42
  • A few things that are unclear right now from your code: (1) why your function have parameter `Request` and never uses it? (2) why your function doesn't return anything? (3) why do you use a `while 1` loop without ever `break`ing? (4) what is the purpose of `count`/`counter`? – Tomerikoo Feb 12 '20 at 14:55
  • i'm trying to build a system that will take screenshot of users after every random intervals and then if the user login they should be able to see the list of their screenshot – fyunusa Feb 12 '20 at 15:48
  • the request was because i wanted to use it in my django views.py that's why, again count/counter was also used before but i have commented it out and forgot to do that here – fyunusa Feb 12 '20 at 15:53
  • Please [edit] that in the question so it is easier for other people to see. Also, make sure you have a [mre] and a well-defined problem statement with your actual and expected outputs – Tomerikoo Feb 12 '20 at 16:01

1 Answers1

0

I think all you're missing is a list to hold all the screen shots and to return those screenshots from the function. Something like:

def takeScreenshot():
    ...
    screenshot_path = r'C:\Users\ZainbaMuhdYunus\Desktop\shots2\shot' + full_name
    myScreenshot.save(screenshot_path)
    return screenshot_path

screenshots = []
while True:
     screenshots.append(takeScreenshot())

screenshots is a list holding all the screenshots' paths

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61