Create light trail effect by python

go.fly
2 min readJun 8, 2021

--

What is light trail?

As its name suggests, light trail is the trail of light which we can capture by using a slow shutter as light moves. — www.canon.com

light trail in the night

However, this is not an article for shot light trail photo, but for generating light trail video. Some thing as below:

video light trail

The difference between two videos is, the second one works as long time exposure, each frame is the “accumulated” results of previous frames. (motion trail?)

How to create this effects?

If I have no experience of python programming, I will use AE’s echo feature, which will show limited numbers of previous frames, as below video(AE 2020 can set max 10 previous frames):

With some basic python library, this effect is very easy to implement.

First, we need a library to read each frame of the video. I use opencv for this article, it can support most of famous video format(H264), and contains useful API to manipulate frame:

OpenCV (Open Source Computer Vision Library) is an open source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. — https://opencv.org/about/

import cv2 
import sys
import numpy as np
cap = cv2.VideoCapture(infile)
fourcc = cv2.VideoWriter_fourcc('H', '2', '6', '4')
vw = cv2.VideoWriter(outfile, fourcc, fps, size)
tmp = None
while cap.isOpened():
ret,frame = cap.read()

Then, do the “accumulate” work to imitate camera exposure. Create a stepped frame, each pixel is the lightest part of previous. In the PS world, it called lighten. In the opencv, you can use max() function, and write each new frame into output video:

tmp = np.maximum(tmp, frame)
vw.write(tmp)

Finally, release video reader & writer, data will auto sync to disk:

cap.release()
vw.release()

Final thoughts

Doing PS “lighten” operation is just a tiny function of opencv library. We can see lots of new apps and website using it for face beauty and picture auto post processing. Maybe we can just write another photoshop by python and opencv.

--

--

go.fly
go.fly

Written by go.fly

Drone photographer, traveler, hacker

No responses yet