moonPhase

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit a8bb26286a8f4863a7b907e0c2de85cb2f6a9f8d
parent f824bfae0711096c15cf53fb37ede78179129d48
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date:   Sun, 29 Dec 2019 17:59:03 +1100

Added arguments, ffmpeg support and thinning, general code cleanup

Diffstat:
Mmoonphase.py | 57+++++++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 45 insertions(+), 12 deletions(-)

diff --git a/moonphase.py b/moonphase.py @@ -1,27 +1,58 @@ import urllib.request as req import os import imageio +import argparse +import subprocess + +# Argument parser +parser = argparse.ArgumentParser("Download Moon Images and Create Video") +parser.add_argument("-t", "--thinning", default=1, help="Frequency of images to download, e.g. every 10th image") +parser.add_argument("-f", "--ffmpeg", action='store_true', help="Use ffmpeg to create video") +args = parser.parse_args() + def addZeros(imageNumber): + """Add leading zeros to a number, up to 4 digits.""" numAdd = 4 - len(str(imageNumber)) return "0" * numAdd + str(imageNumber) def getImage(imageNumber, directory): - URL = "https://svs.gsfc.nasa.gov/vis/a000000/a004600/a004604/frames/730x730_1x1_30p/moon." + imageNumber + ".jpg" + """Download an image given an image to an output directory""" + URL = ( + "https://svs.gsfc.nasa.gov" + "/vis/a000000/a004600/a004604/frames/" + "730x730_1x1_30p/moon.{}.jpg" + ).format(imageNumber) + imageName = directory + imageNumber + ".jpg" req.urlretrieve(URL, imageName) return -def makeVideo(directory): - files = [image for image in os.listdir(directory) if ".jpg" in image] - images = [] - - for frameFile in files: - images.append(imageio.imread(directory + frameFile)) - imageio.mimsave(directory + 'movie.gif', images) - return +def makeVideo(directory, ffmpeg=False): + """Create a video with the downloaded images""" + + # Get a list of all the files + files = [image for image in os.listdir(directory) if ".jpg" in image] + + if ffmpeg: + frameRate = str(len(files)/60) + subprocess.run( + ["ffmpeg", + "-r", frameRate, + "-pattern_type", "glob", + "-i", "./images/*.jpg", + "-vb", "20M", "moon.mp4"], check=True) + + else: + # Save each frame to a list + images = [] + for frameFile in files: + images.append(imageio.imread(directory + frameFile)) + + imageio.mimsave(directory + 'movie.gif', images) + return -def main(startNumber=1, thinning=10): +def main(startNumber=1, thinning=1, ffmpeg=False): directory = 'images/' @@ -40,8 +71,10 @@ def main(startNumber=1, thinning=10): break print("Making video...") - makeVideo(directory) + makeVideo(directory, ffmpeg) return -main(1, 20) + +main(1, int(args.thinning), args.ffmpeg) +