Timelapse of the sun eclipse with the help of python

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")]

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

Update 22.Apr. 2015:

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


Passion for sports with boards, climbing, mountain biking, traveling, photography, videography and software engineering. /n/n Heilbronn // Germany


  1. Hello Michi!
    Thanks for writing this script. I have the same problem as you did with my solar eclipse timelapse(shot from my balcony here in the north of Sweden). I cant seem to get the script to work tho as i get the following error when i run it:
    “C:\Python34>python.exe align.py
    File “align.py”, line 21
    print “Sunsize %d px” % sunSize
    SyntaxError: Missing parentheses in call to ‘print’

    Any ideas? my picturefiles are in 20mpix jpg’s and are in the right order.
    I´v also typed in the correct folders for the source and target files.

    Cheers! Åke

    • Hi Åke,

      my script is written for Python 2 – you have installed version 3. I will put this into the preconditions. Thanks!

      Please post your video when it is ready 🙂

      Cheers Michi

  2. Hi Michi,

    First of all, thanks for this great script. Exactly what I was looking for. I’ve tried aligning my 650 pictures with ImageJ (and Fiji)’s plugin “Stackreg”, but no success.

    However, I encounter an issue with your script. Forgive me, I’m totally noob with Python. Here is what I did:
    – Install Python 2.7 for Windows (x64)
    – Install PIL 1.1.7, as I see it’s required in your script
    – Change the input and output folders in your script (using “\\” to separate subfolders)
    – Launch cmd.exe, and type “aligneclipse.py”, which is your script that I saved in a folder already recognized in Windows PATHS

    I get the following error:
    Sunsize 425 px
    Saving to H:\Photographie\Albums divers\Publication web\Eclipse\Align/DSC02171.j
    Traceback (most recent call last):
    File “C:\Python27\Scripts\aligneclipse.py”, line 35, in
    File “C:\Python27\lib\site-packages\PIL\Image.py”, line 1433, in save
    fp = __builtin__.open(fp, “wb”)
    IOError: [Errno 2] No such file or directory: ‘H:\\Photographie\\Albums divers\\
    Publication web\\Eclipse\\Align/DSC02171.jpg’

    Note that the eclipse was almost total where I shot it, so some of the pictures will show a very thin border for the sun (and the border will be at the top, not bottom like in your case).

    Any ideas? Thanks a lot!

    • I’m stupid. It works perfectly.

      The only issue is that I renamed the output folder as “Aligned” instead of the “Align” that was written in the script.

      Your script is awesome.

        • Finally, there’s still a problem. The 200 px border is applied only to the visible sun, not the total sun as shown in the first picture of the sequence. This results in two issues in the cropped images. I’ve tried to explain it in English but I’m not a native speaker, and I’m not even sure I figure out what is actually happening with the crops. Can you tell me what software you use to compile the timelapse, so that I can do one and show you directly the output with the issue?

          • I use Final Cut Pro X for the timelapse. I will send you an email so you can send me a few pictures to help you finish your timelapse 🙂

          • Thanks Michi, I’ll send you a few sample pictures by email so that you can see what’s happening. Long story short: the final resolution of the cropped images varies, because the border use the visible sun as a reference, not the full sun shown in the first picture of the folder. When the eclipse is almost full (only a thin border of the sun on the top), it makes the sun to go down in the cropped picture, keeping the bottom magin to 200 px between bottommost pixel of the visible sun and the bottommost line of the cropped picture.

            I am running Windows so Final Cut Pro X is not possible, but I tried Photolapse (a very simple and efficient timelapse maker), but the output movie only shows the first frame (probably because the software does not know how to handle pictures of varying resolution).

            Thanks for your time!

        • Here is what I got Michi:


          The aligment of the sun is not perfect, as you can see. To overcome the issue explained yesterday in my rather long comment, I had to rotate all my pictures by 180° so that the part of the sun that is visible throughout the eclipse is at the bottom. It prevented the sun to move around too much in the crops. I don’t know Python, but I guess your script only works when the reference border is at the bottom.

          For reference, here is the whole workflow. I rotated the output pictures by 180° again to restore their original orientation. I eventually used ImageMagick and Mogrify, because your script created pictures of different resolutions and it was not good for my video-editing softwares. I used the following command-line to normalize all pictures to 1920×1080 while keeping the original picture at the center (ImageMagick and Mogrify are the only ways I found to do that):

          mogrify -extent 1920×1080 -gravity Center -fill black *.jpg

          For some reason, the “-fill black” argument filled the canvas with white instead of white. Therefore, I had to use an additional command to replace white with black:

          mogrify -fill “#000000” -opaque “#FFFFFF” *.jpg

          I loaded all the pictures in AVIDemux, and did the time-lapse with it. I couldn’t get better results with LRTimelapse, though I would have liked its antiflicker feature. I’m probably doing it wrong with LRTimelapse, but the workflow is rather complicated with it and I’m not sure I master it; plus it misread the resolution of my 1920×1080 pictures and re-extended them in a weird manner, resulting temporary files of 1096×1301 resolution and a final video output of 1920×608.

    • Hi Mathieu,

      in windows, you will only need one backslash (\) for selecting the subfolders.

      Change “outfile = “%s/%s” % (outputDir, img)” to “outfile = “%s\%s” % (outputDir, img)” (Backslash for windows)

      Please let me know if this helps you!

      Also – share your timelapse when it is finished 🙂

      Cheers Michi

      • Hi there,

        I’ve sent you a few days ago the final timelapse as well as the procedure I’ve used, but it seems you haven’t validated my message. Let me know if you want some additional info.

        • Hi Mathieu,

          sorry I am a litte bit busy at the moment. I have already implemented a better algo for you. Will share it with you in the next days.

          Cheers Michi

          • Hi Michi,

            Thanks for the new script! I’m sorry for answering this late, I’ve been very busy and could not try it. I’ve tried it today. Now I can run it on my pictures without all the steps I detailed in my previous message, and without prior rotating. However, I still have issues with stabilization. It seems that the script has trouble centering the sun on some pictures, see by yourself:


            What would you suggest? I don’t see what could help here apart from manually recentering each picture (and there are 650, so I give up :/).


          • Hi Mathieu,

            strange behaviour. Can you send me all your images via wetransfer?

            Cheers Michi

  3. Hi Michi!

    Thank you very much for this script. It really useful for me!
    Is it possible, somehow, that the user can provide a reference point in the first image and then align all the rest according to this reference point?


  4. Hi Michi!

    Let’s say that there is a plane crossing the solar disk.
    Would it be possible to align the images such that the plane is stable and the Sun seems to be moving?


Leave a Reply