6

I'm relatively new to python and know very little syntax, but I'm willing to learn as much as possible. Simply put I want to use the save feature in PIL to save a .png with the file's name being the current date and time. This may be complicated by the fact that I'm not using PIL directly, but through the Videocapture module, but i doubt it. this is my code that works

from VideoCapture import Device
cam = Device()
cam.saveSnapshot('C:\Users\Myname\Dropbox\Foldes\image.png', timestamp=3, boldfont=1, textpos='bc')

Its short, but it does what I need it to. I realize Datetime will need to be imported, But I can't get the data as the name without errors. yes i have tried the str() command. Any help will be greatly appreciated.

robocop408
  • 63
  • 1
  • 1
  • 3

1 Answers1

9
'C:\Users\Myname\Dropbox\Foldes\image.png'

In strings in Python, backslashes have special meaning so you need to treat them differently. You can either use two of them instead of one...

'C:\\Users\\Myname\\Dropbox\\Foldes\\image.png'

...or you can put an r before the string (as long as it doesn't end with a backslash)

r'C:\Users\Myname\Dropbox\Foldes\image.png'

To generate a string containing the current day in YYYY-MM-DD-HH:MM format, we can use the datetime module like this. To format the timestamp differently, consult the documentation here.

import datetime
date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M")

As a shorter alternative, you could use the similar time module instead:

import time
date_string = time.strftime("%Y-%m-%d-%H:%M")

After this, you should just be able to do

cam.saveSnapshot(r'C:\Users\Myname\Dropbox\Foldes\image-' + date_string + '.png',
                 timestamp=3, boldfont=1, textpos='bc')

to save the image with the datetime in the filename. (I have split the function call over two lines for readability, see this question for some explanation of how this works.)

Community
  • 1
  • 1
Jeremy
  • 1
  • 85
  • 340
  • 366
  • **from time import strftime** will be simpler – eyquem Jun 13 '11 at 08:20
  • @eyquem Thanks for the suggestion, I have added it to the answer. – Jeremy Jun 13 '11 at 08:34
  • **from time import strftime** avoids to import all the content of the module **time** – eyquem Jun 13 '11 at 10:41
  • @eyquem I think that's incorrect; Python loads modules all-at-once, the import options only control which names are added to the current namespace (in this case, `time` vs `strftime`). – Jeremy Jun 13 '11 at 10:46
  • @Jeremy Banks I didn't know. That solves an interrogation I had: how does it work when an imported function of a module needs other functions of the module that are not specifically imported by another **from module import ...** But I would like to have official reference on this point. – eyquem Jun 13 '11 at 12:27
  • Thanks for the quick response, but for future record. the 'C:\Users\Myname\Dropbox\Foldes\image.png' worked just fine – robocop408 Jun 15 '11 at 02:39
  • @robocop That's interesting. Looking into it, it apparently works fine in most cases (`"C:\Users"`), but fails for things like `"C:\Users\nblah"` because `\n` represents a newline. – Jeremy Jun 15 '11 at 02:50
  • 4
    For anyone trying to use this script, the colon in the datetime string was causing an error. I replaced it with a space, and even though it looks funny, the script runs without error. – robocop408 Jun 29 '11 at 03:36
  • 1
    note that `:` is not a valid filename character in Windows, but is valid in Linux. – endolith Apr 30 '12 at 06:18