Timelapse of the sun eclipse with the help of python

Posted on 21. March 2015

On the 20th March 2015, there was a partial sun eclipse. I wanted to make a timelapse. I put my camera on a tripod, placed the sun in the lower left corner of the display and started the timelapse mode which made a photo every 5 seconds. After 20 muinutes, I realized that the sun will soon move out of the picture.

Because the sun eclipse took a little bit more then 2 hours, I had to readjust the tripod every 30 minutes. Now I had 1600 pictures. A timelapse of these images will show a bouncing sun. Not really good. So I had to align the images. Manually, this whould have been a lot of work. So I wrote a small Python script which cuts the sun out so the images will be aligned.


– You will need the jpeg files in the correct order in one folder.
– Python 2 (with PIL installed)

The Python script

Enter the inputDir (your jpeg files) and outputDir (where the cropped images should be saved) manually in the script.

Then save the script, execute and let the magic happen.

The script searches in the first image for the border of the sun and calculates its size. Since this was a partial sun eclipse, the bottom of the sun will always be visible so we can use this point as reference. Also, everything will be black.

Now we crop around the sun with a border of 200px and save the image with the same filename in the outputDir folder. When the script is done, we have wonderful aligned images of the sun. Load the images in your video editing software and create the timelapse.

import os, sys from PIL import Image, ImageOps from os import path import imghdr inputDir = "img/input" outputDir = "img/output" sunSize = 0 def treshold(x): if x < 100: x = 0 return x def suncrop(image, border): sunBorders = image.point(treshold).getbbox() global sunSize if sunSize == 0: sunSize = sunBorders[3] - sunBorders[1] print "Sunsize %d px" % sunSize sunBorderBorders = (sunBorders[0]-border, sunBorders[3]-sunSize-border, sunBorders[2]+border, sunBorders[3]+border) return image.crop(sunBorderBorders) sunSize = 0 files = [f for f in os.listdir(inputDir) if f.endswith(".jpg")] files.sort() for img in files: im = Image.open("%s/%s" % (inputDir, img)) imtrim = suncrop(im, 200) outfile = "%s/%s" % (outputDir, img) print "Saving to %s" % outfile imtrim.save(outfile)

Update 22.Apr. 2015:

I have changed the script to be more accurate. You can find it here.

Made with ♥️ and Gatsby © 2020