{"id":129,"date":"2023-05-27T14:56:00","date_gmt":"2023-05-27T14:56:00","guid":{"rendered":""},"modified":"2024-01-08T13:27:10","modified_gmt":"2024-01-08T13:27:10","slug":"a-python-time-lapse-program","status":"publish","type":"post","link":"https:\/\/doctor-dark.co.uk\/blog\/a-python-time-lapse-program\/","title":{"rendered":"A Python time-lapse program."},"content":{"rendered":"<h2 style=\"text-align: justify;\"><span style=\"font-family: Tahoma;\">A free program&#8230;<\/span><\/h2>\n<p style=\"text-align: justify;\"><span style=\"font-family: Tahoma;\">This is the Python code I cobbled together to make a time-lapse movie of my rather exciting flowering cactus. I&#8217;m sure this has been done better by lots of people. It runs on a Raspberry Pi Zero, with not much memory, and no online storage, so it sends the pictures to another Pi Zero, called PiBigStore, which happens to have a 2 Terabyte USB drive. Help yourself to a copy, if you like. Change the server name, and password, obviously. If you know ways this can be improved, feel free to comment.<\/span><\/p>\n<p style=\"text-align: justify;\"><\/p>\n<div style=\"text-align: left;\"><span style=\"font-family: courier;\"># Time lapse pictures<br \/><\/span><span style=\"font-family: courier;\">import os<br \/><\/span><span style=\"font-family: courier;\">import time<br \/><\/span><span style=\"font-family: courier;\">import ftplib<br \/><\/span><span style=\"font-family: courier;\">from picamera import PiCamera<br \/><\/span><span style=\"font-family: courier;\">import schedule<\/span><\/div>\n<div style=\"text-align: left;\"><span style=\"font-family: courier;\"><br \/><\/span><span style=\"font-family: courier;\">def send_to_PiBigStore():<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; hour = int(time.strftime(&#8220;%H&#8221;))<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; #print(hour)<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; if hour &lt; 7 or hour &gt; 21:<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; time.sleep(250)<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; return<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp;&nbsp;<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; file_name = &#8220;cactus&#8221; + time.strftime(&#8220;%Y%m%d-%H%M%S&#8221;) + &#8220;.jpg&#8221;<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; camera.capture(&#8220;\/var\/tmp\/&#8221; + file_name)<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp;&nbsp;<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; connected = True<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; ftp = ftplib.FTP()<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; try:<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; ftp.connect(&#8220;PiBigStore&#8221;)<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; except ftplib.all_errors:<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; connected = False<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; print(&#8220;Couldn&#8217;t connect to PiBigStore.&#8221;)<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; ftp.quit()<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; try:<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; ftp.login(&#8220;pi&#8221;,&#8221;password goes here&#8221;)<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; except ftplib.all_errors:<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; connected = False<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; print (&#8220;Failed to login to PiBigStore server.&#8221;)<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; ftp.quit()<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp;&nbsp;<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; if connected:<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; ftp.cwd(&#8220;\/media\/pidrive\/data\/cactus\/&#8221;)<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; ftp.storbinary(&#8216;STOR &#8216;+file_name, open(&#8220;\/var\/tmp\/&#8221;+file_name, &#8220;rb&#8221;))<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; &nbsp; &nbsp; print (&#8220;Sent to PiBigStore &#8220;, file_name)<\/span><span style=\"font-family: courier;\"><br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; ftp.quit()<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; os.remove(&#8220;\/var\/tmp\/&#8221;+file_name)<\/span><\/div>\n<div style=\"text-align: left;\"><span style=\"font-family: courier;\"><br \/><\/span><span style=\"font-family: courier;\"># Main loop<br \/><\/span><span style=\"font-family: courier;\">schedule.every(5).minutes.do(send_to_PiBigStore)<br \/><\/span><span style=\"font-family: courier;\">camera = PiCamera()<br \/><\/span><span style=\"font-family: courier;\">camera.rotation = 90<\/span><\/div>\n<div style=\"text-align: left;\"><span style=\"font-family: courier;\"><br \/><\/span><span style=\"font-family: courier;\">while True:<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; schedule.run_pending()<br \/><\/span><span style=\"font-family: courier;\">&nbsp; &nbsp; time.sleep(10)<\/span><\/div>\n<div><\/div>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" style=\"margin-left: auto; margin-right: auto;\">\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><a href=\"https:\/\/doctor-dark.co.uk\/blog\/wp-content\/uploads\/2023\/05\/20230525_181316.jpg\" style=\"margin-left: auto; margin-right: auto;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" data-original-height=\"4624\" data-original-width=\"2084\" height=\"400\" src=\"https:\/\/doctor-dark.co.uk\/blog\/wp-content\/uploads\/2023\/05\/20230525_181316-135x300.jpg\" width=\"180\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\"><span style=\"font-family: Tahoma;\">A foot-tall cactus on a windowsill, with a Raspberry Pi Zero with camera, mounted on a Lego tower.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/p>\n<div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A free program&#8230; This is the Python code I cobbled together to make a time-lapse movie of my rather exciting flowering cactus. I&#8217;m sure this has been done better by lots of people. It runs on a Raspberry Pi Zero, with not much memory, and no online storage, so it sends the pictures to another &hellip; <a href=\"https:\/\/doctor-dark.co.uk\/blog\/a-python-time-lapse-program\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A Python time-lapse program.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":333,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-raspberry-pi"],"jetpack_featured_media_url":"https:\/\/doctor-dark.co.uk\/blog\/wp-content\/uploads\/2023\/05\/20230525_181316.jpg","_links":{"self":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":2,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"predecessor-version":[{"id":524,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/posts\/129\/revisions\/524"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/media\/333"}],"wp:attachment":[{"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/doctor-dark.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}