element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
STEM Academy
  • Learn
  • Learning Center
  • STEM Academy
  • More
  • Cancel
STEM Academy
Forum Python Question - Formatting
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join STEM Academy to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Verified Answer
  • Replies 4 replies
  • Answers 1 answer
  • Subscribers 50 subscribers
  • Views 1134 views
  • Users 0 members are here
  • python
  • raspberypi
  • stem space
Related

Python Question - Formatting

mikedavis
mikedavis over 10 years ago

I am working on a time lapse project, and I would like to be able to save my files with a time stamp (something like YYYYMMDD-HMS.jpg).  So I used the code below:

 

import time
import picamera

VIDEO_DAYS = 1
FRAMES_PER_HOUR = 360
FRAMES = FRAMES_PER_HOUR * 3 * VIDEO_DAYS


file = time.strftime("%Y%m%d-%H%M%S")

def capture_frame(frame):
    with picamera.PiCamera() as cam:
        time.sleep(2)
        cam.led = False #Turn off the Red LED
        cam.capture('/home/pi/camera/'+file+'.jpg' % (frame))

# Capture the images
for frame in range(FRAMES):
    # Note the time before the capture
    start = time.time()
    capture_frame(frame)
    # Wait for the next capture. Note that we take into
    # account the length of time it took to capture the
    # image when calculating the delay
    time.sleep(
        int(60 * 60 / FRAMES_PER_HOUR) - (time.time() - start)
    )

I end up getting this error message.

 

File "camera.py", line 20, in <module>
    capture_frame(frame)
  File "camera.py", line 14, in capture_frame
    cam.capture('/home/pi/camera/'+file+'.jpg' % (frame))
TypeError: not all arguments converted during string formatting

I feel like I am missing something simple or obvious, but I am not sure what that is, and my inexperience is starting to show.

 

Any help would be greatly appreciated, thank you!

  • Sign in to reply
  • Cancel

Top Replies

  • balearicdynamics
    balearicdynamics over 10 years ago in reply to mikedavis +1 verified
    You shown me just what I was trying to explain ! cam . capture ( '/home/pi/Desktop/frame%03d.jpg' % frame ) you see that the % frame is a variable that can't fit space (in your code) in the compound string…
  • balearicdynamics
    0 balearicdynamics over 10 years ago

    I am not a python expert, but the following line sounds strange:

     

    cam.capture('/home/pi/camera/'+file+'.jpg' % (frame)) 


    first of all because there are no spaces between '+' and because I suppose that the last % (frame) refers to a value that is not chained to the string. Or I am wrong ?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • mikedavis
    0 mikedavis over 10 years ago in reply to balearicdynamics

    I think that it is not chained to the string.  I believe that it basically counts the number of pictures that are taken in that portion of the code.  The original code I used (and always works) is from the RaspberryPi.org site.

     

    import time
    import picamera

    VIDEO_DAYS = 5
    FRAMES_PER_HOUR = 1
    FRAMES = FRAMES_PER_HOUR * 24 * VIDEO_DAYS

    def capture_frame(frame):
       with picamera.PiCamera() as cam:
      time.sleep(2)
      cam.capture('/home/pi/Desktop/frame%03d.jpg' % frame)

    # Capture the images
    for frame in range(FRAMES):
       # Note the time before the capture
      start = time.time()
      capture_frame(frame)
       # Wait for the next capture. Note that we take into
       # account the length of time it took to capture the
       # image when calculating the delay
      time.sleep(
      int(60 * 60 / FRAMES_PER_HOUR) - (time.time() - start)
       )

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • balearicdynamics
    0 balearicdynamics over 10 years ago in reply to mikedavis

    You shown me just what I was trying to explain !

     

    cam.capture('/home/pi/Desktop/frame%03d.jpg' % frame)


    you see that the % frame is a variable that can't fit space (in your code) in the compound string. In the origina the /frame%03d.jpg Means:


    Format three integer digits left-aligned with zeroes in this point, then at the end of the string you have the variable that is used creating the file name.


    I suggest that you create before a string '/home/pi/camera/' + file + '%03d.jpg' so you don't stress the program to recreate for a lot of times always the same string that is the formatter


    Then place the string in your command with the %frame that is the replacement variable for the formatter.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Reject Answer
    • Cancel
  • mikedavis
    0 mikedavis over 10 years ago in reply to balearicdynamics

    THANKS!  That did it!  I also showed me that the file names are going to be named with the time just once, as opposed to every time.  That is good enough for me!

     

    Thank you!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube