Advertisement
DeaD_EyE

TimeElapse (untested)

Aug 29th, 2021 (edited)
1,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import time
  4. from argparse import ArgumentParser
  5. from itertools import count
  6. from pathlib import Path
  7. from typing import Iterator, Optional
  8.  
  9. from picamera import PiCamera
  10.  
  11. FORMATS = "jpeg png gif bmp yuv rgb rgba bgr bgra".split()
  12.  
  13.  
  14. def setup_camera(width, height):
  15.     camera = PiCamera()
  16.     camera.resolution(width, height)
  17.     return camera
  18.  
  19.  
  20. def main(
  21.     interval: float,
  22.     directory: Path,
  23.     name: str,
  24.     start: int,
  25.     img_format: str,
  26.     frames: Optional[int],
  27.     height: int,
  28.     width: int,
  29.     preview: Optional[bool],
  30. ):
  31.     camera = setup_camera(width, height)
  32.     iterator = range(start, start + frames) if frames else count(start)
  33.  
  34.     if preview:
  35.         camera.start_preview()
  36.         print("Preview", width, "x", height, format)
  37.  
  38.     with camera:
  39.         for number in iterator:
  40.  
  41.             target = directory / f"{name}_{number:04d}.{img_format}"
  42.             time.sleep(interval)
  43.             camera.capture(str(target))
  44.             print(target)
  45.  
  46.  
  47. def get_args():
  48.     parser = ArgumentParser()
  49.     parser.add_argument(
  50.         "directory", type=Path, help="Base directory where to put the files"
  51.     )
  52.     parser.add_argument("name", help="name of file without .jpg suffix")
  53.     parser.add_argument(
  54.         "-i", "--interval", default=2.0, type=float, help="interval in seconds"
  55.     )
  56.     parser.add_argument(
  57.         "-f",
  58.         "--format",
  59.         dest="img_format",
  60.         default="jpg",
  61.         choices=FORMATS,
  62.         help="Format of file",
  63.     )
  64.     parser.add_argument("--frames", default=None, type=int, help="Limit the frames")
  65.     parser.add_argument(
  66.         "--start", default=0, type=int, help="Offset where to start the counting"
  67.     )
  68.     parser.add_argument("--width", default=4056, type=int, help="Width of image")
  69.     parser.add_argument("--height", default=3040, type=int, help="Height of image")
  70.     parser.add_argument("-p", "--preview", action="store_true", help="Preview")
  71.     return parser.parse_args()
  72.  
  73.  
  74. if __name__ == "__main__":
  75.     args = get_args()
  76.  
  77.     try:
  78.         main(**vars(args))
  79.     except KeyboardInterrupt:
  80.         pass
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement