Unable to Save Screenshots with dynamic name using image.grab() in PIL library

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Raj4perl
    New Member
    • Aug 2014
    • 12

    Unable to Save Screenshots with dynamic name using image.grab() in PIL library

    import sys
    import time
    import os
    from PIL import ImageGrab

    print("Enter time interval to take screenshot")
    time_int = 3
    print("Time interval entered is %(time)d" %{"time":time_i nt})
    img = ImageGrab.grab( )
    localtime = time.asctime( time.localtime( time.time()) )
    localtime = localtime.repla ce(" ","_")
    environ = os.environ
    Files_Dir = r'images'
    save_path = r'C:\\results'
    File_name = localtime+'.jpg '
    print("Filename is %(File_name)s" %{"File_name":F ile_name})
    File_path = os.path.join(sa ve_path, Files_Dir, File_name)
    directory = os.path.join(sa ve_path, Files_Dir)
    print("path where the image saved is %(path)s" %{"path":File_p ath})
    if not os.path.exists( directory):
    try:
    os.makedirs(dir ectory)
    except OSError as exception:
    if exception.errno !=errno.EEXIST:
    raise
    img.save(File_p ath, "JPEG")




    OSError: [Errno 22] Invalid argument: 'C:\\results\\i mages\\Thu_Aug_ _4_15:40:33_
    2016.jpg'
Working...