Time Collapsing* Photos With OpenCV

Adam Derewecki
Jul 10 · 2 min read
Time Collapse of San Francisco

A few years ago, I had a camera looking out over San Francisco. It was hooked up to a Raspberry Pi that snapped a photo every minute and posted it. When there was a good sunset, I went back and made a time lapse video from the photos:

After a while of collecting photos, I started thinking about what else I could do. Inspired by photo projects that show the same scene throughout different seasons of the year, I decided to create a single photo from 24 hours of photos using OpenCV and Python:

import numpy as np
import cv2

filenames = ["file1.jpg", ... ]
columns = []

for i, filename in enumerate(filenames):
img: np.array = cv2.imread(f"p/{filename}")
height: int = len(img)
width: int = len(img[0])
column_start = 2 * i
column_end = 2 * i + 2
print(f"filename: {filename}, height: {height}, width: {width}, {column_start}-{column_end}")
column = img[0:height, column_start:column_end]

matrix = [[] for _ in range(height)]

for row_idx in range(height):
row = []
for col_idx in range(len(columns)):
pixels = columns[col_idx][row_idx]
matrix[row_idx] += list(pixels)

new_img = np.array([np.array(list(row)) for row in matrix])
cv2.imwrite('output.jpg', new_img)

I wouldn’t call the resulting image pretty, but I do think it’s interesting.

*It’s not clear if time collapsing is the proper term for this technique, but it makes sense to me

Adam Derewecki

Written by

Hi! I’m Adam. I live in San Francisco, write code, take pictures, and practice yoga.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade