Hi
I would like my python script take screenshot of my desktop every 15 mints and saving in my home dir in pic folder.but I get error?

import os
import sys
import time
import ImageGrab
environ=os.environ
img = ImageGrab.grab()
FILES_DIR = 'pic'
SAVE_PATH = os.path.join(environ['HOMEDRIVE'], environ['HOMEPATH'])
LOGFILE_NAME = 'test.png'       
LOGFILE_PATH = os.path.join(SAVE_PATH, FILES_DIR, LOGFILE_NAME)
img.save(SAVE_PATH)


//Traceback (most recent call last):
  File "C:\Users\linux\Desktop\screenshot1.py", line 11, in <module>
    img.save(SAVE_PATH)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1423, in save
    raise KeyError(ext) # unknown extension
KeyError: ''//

Save as test.jpg .

Before I saved as test.jpg but I got the same error? I don't know why!!!

//Traceback (most recent call last):
  File "C:\Users\linux\Desktop\screenshot1.py", line 11, in <module>
    img.save(SAVE_PATH)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1423, in save
    raise KeyError(ext) # unknown extension
KeyError: ''//
>>> 

Traceback (most recent call last):
File "C:UserslinuxDesktopscreenshot1.py", line 11, in <module>
img.save(SAVE_PATH)

Print SAVE_PATH to see what name the program is trying to use (not the one you think it is). Also you can use
from os import environ
My computer likes environ['HOME'] instead of 'HOMEPATH' but it may be different on different OS's.

commented: good +14

The home directory is best obtained with

home_dir = os.path.expanduser("~")

It is cross-platform !

Thanks for answer.
Im using win7 and python 2.7.6
But still get the same error.
Im importing environ and using home_dir=os.path.expanduser("~")!
Anyone tried to run it on windows?

Is there better way to take screenshoot and save it in the folder in home dir?

import os
import sys
import time
import ImageGrab
import Image
from os import environ
#environ=os.environ
img = ImageGrab.grab()
FILES_DIR = 'pic'
#SAVE_PATH = os.path.join(environ['HOMEDRIVE'], environ['HOMEPATH'])
SAVE_PATH = os.path.expanduser("~")
LOGFILE_NAME = 'test.jpg'       
LOGFILE_PATH = os.path.join(SAVE_PATH, FILES_DIR, LOGFILE_NAME)
img.save(SAVE_PATH)

//Traceback (most recent call last):
  File "C:\Users\linux\Desktop\screenshot1.py", line 14, in <module>
    img.save(SAVE_PATH)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1423, in save
    raise KeyError(ext) # unknown extension
KeyError: ''
>>> //

I think it should be img.save(LOGFILE_PATH)

Thanks Gribouillis you have right.its "img.save(LOGFILE_PATH)"

Now how can I do the script take every 10 mints screenshot on my desktop without overwrite previous picture?

OK we can take a random interval but still overwrite previous picture?How can I fix it?

import os
import sys
import time
import ImageGrab
import Image
from os import environ
import random

while 1:

        # generate a random time between 120 and 300 sec
        random_time = random.randrange(120,300)

        # wait between 120 and 300 seconds (or between 2 and 5 minutes)
        print "Next picture in: %.2f minutes" % (float(random_time) / 60)
        time.sleep(random_time)
        img = ImageGrab.grab()
        FILES_DIR = 'pic'
        SAVE_PATH = os.path.join(environ['HOMEDRIVE'], environ['HOMEPATH'])
        #SAVE_PATH = os.path.expanduser("~")    #It is cross-platform
        LOGFILE_NAME = 'test.jpg'       
        LOGFILE_PATH = os.path.join(SAVE_PATH, FILES_DIR, LOGFILE_NAME)
        img.save(LOGFILE_PATH)

Use a variable name

for n in range(10):
    LOGFILE_NAME = "test{n:0>5}.png".format(n = n)
    print(LOGFILE_NAME)

"""my output -->
test00000.png
test00001.png
test00002.png
test00003.png
test00004.png
test00005.png
test00006.png
test00007.png
test00008.png
test00009.png
"""

Thanks Gribouillis
Do I somthing wrong its still overwrite old picture?

import os
import sys
import time
import ImageGrab
import Image
from os import environ
import random

while 1:

        # generate a random time between 120 and 300 sec
        random_time = random.randrange(120,300)

        # wait between 120 and 300 seconds (or between 2 and 5 minutes)
        print "Next picture in: %.2f minutes" % (float(random_time) / 60)
        time.sleep(random_time)
        img = ImageGrab.grab()
        FILES_DIR = 'pic'
        SAVE_PATH = os.path.join(environ['HOMEDRIVE'], environ['HOMEPATH'])
        #SAVE_PATH = os.path.expanduser("~")    #It is cross-platform
        LOGFILE_NAME = 'test.jpg'       
        LOGFILE_PATH = os.path.join(SAVE_PATH, FILES_DIR, LOGFILE_NAME)
        img.save(LOGFILE_PATH)

        for n in range(10):
            LOGFILE_NAME = "test{n:0>5}".format(n = n)
            print(LOGFILE_NAME)



//>>> ================================ RESTART ================================
>>> 
Next picture in: 2.55 minutes
test00000
test00001
test00002
test00003
test00004
test00005
test00006
test00007
test00008
test00009
Next picture in: 4.65 minutes
test00000
test00001
test00002
test00003
test00004
test00005
test00006
test00007
test00008
test00009
Next picture in: 3.33 minutes//

Obviously you don't understand what the code does. Try

import os
import sys
import time
import ImageGrab
import Image
from os import environ
import random

n = -1
while True:
        n += 1
        # generate a random time between 120 and 300 sec
        random_time = random.randrange(120,300)

        # wait between 120 and 300 seconds (or between 2 and 5 minutes)
        print "Next picture in: %.2f minutes" % (float(random_time) / 60)
        time.sleep(random_time)
        img = ImageGrab.grab()
        FILES_DIR = 'pic'
        SAVE_PATH = os.path.join(environ['HOMEDRIVE'], environ['HOMEPATH'])
        #SAVE_PATH = os.path.expanduser("~")    #It is cross-platform
        LOGFILE_NAME = "test{n:0>5}.png".format(n = n)
        LOGFILE_PATH = os.path.join(SAVE_PATH, FILES_DIR, LOGFILE_NAME)
        img.save(LOGFILE_PATH)

Thanks Gribouillis
Now Im understand you.
Thanks again for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.