Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import time
- from argparse import ArgumentParser
- from itertools import count
- from pathlib import Path
- from typing import Iterator, Optional
- from picamera import PiCamera
- FORMATS = "jpeg png gif bmp yuv rgb rgba bgr bgra".split()
- def setup_camera(width, height):
- camera = PiCamera()
- camera.resolution(width, height)
- return camera
- def main(
- interval: float,
- directory: Path,
- name: str,
- start: int,
- img_format: str,
- frames: Optional[int],
- height: int,
- width: int,
- preview: Optional[bool],
- ):
- camera = setup_camera(width, height)
- iterator = range(start, start + frames) if frames else count(start)
- if preview:
- camera.start_preview()
- print("Preview", width, "x", height, format)
- with camera:
- for number in iterator:
- target = directory / f"{name}_{number:04d}.{img_format}"
- time.sleep(interval)
- camera.capture(str(target))
- print(target)
- def get_args():
- parser = ArgumentParser()
- parser.add_argument(
- "directory", type=Path, help="Base directory where to put the files"
- )
- parser.add_argument("name", help="name of file without .jpg suffix")
- parser.add_argument(
- "-i", "--interval", default=2.0, type=float, help="interval in seconds"
- )
- parser.add_argument(
- "-f",
- "--format",
- dest="img_format",
- default="jpg",
- choices=FORMATS,
- help="Format of file",
- )
- parser.add_argument("--frames", default=None, type=int, help="Limit the frames")
- parser.add_argument(
- "--start", default=0, type=int, help="Offset where to start the counting"
- )
- parser.add_argument("--width", default=4056, type=int, help="Width of image")
- parser.add_argument("--height", default=3040, type=int, help="Height of image")
- parser.add_argument("-p", "--preview", action="store_true", help="Preview")
- return parser.parse_args()
- if __name__ == "__main__":
- args = get_args()
- try:
- main(**vars(args))
- except KeyboardInterrupt:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement