An Exploration Into Papers With Code

Add A New Dimension To Your Photos Using Python

Breathe New Life Into Your Photos With A 3D Ken Burns Effect

Dylan Roy
Dylan Roy
Nov 19, 2020 · 6 min read
Image for post
Image for post
Image by Author

Some Backstory

Concepts

Image for post
Image for post
CC BY-SA 3.0, via Wikimedia Commons

DIY

Setup Environment and Install Requirements

!git clone https://github.com/sniklaus/3d-ken-burns.git# Move into the downloaded repository
%cd 3d-ken-burns
# Make a directory for the videos
!mkdir videos
video_dir = "./videos/"
# Install dependencies
!pip install moviepy gevent

Upload Photos

from google.colab import filesuploads = files.upload()for filename in uploads.keys():
!mv ./$filename ./images/$filename

Convert Your Photos

python autozoom.py --in {input_image}.jpg --out {output_video}.mp4
!for image in ./images/*; do python autozoom.py --in $image --out ./videos/$(basename $image | cut -f1 -d '.').mp4; done

View Your Video

import os
from base64 import b64encode
from IPython.display import HTML
import ipywidgets as widgets
def video(path):
mp4 = open(path,'rb').read()
data_url = "data:video/mp4;base64," + b64encode(mp4).decode()
return HTML(f'<video width=600 controls loop> <source src="{data_url}" type="video/mp4"></video>')
files_list = os.listdir(video_dir)
video_list = widgets.Dropdown(
options=files_list,
value="" if not files_list else files_list[0],
description='Video:',
disabled=False,
)
display(video_list)

Convert Mp4 to Animated GIF

import imageio
import os, sys
class TargetFormat(object):
GIF = ".gif"
MP4 = ".mp4"
AVI = ".avi"
def convertFile(inputpath, targetFormat):
"""Reference: https://gist.github.com/michaelosthege/cd3e0c3c556b70a79deba6855deb2cc8"""
outputpath = os.path.splitext(inputpath)[0] + targetFormat
print("converting\r\n\t{0}\r\nto\r\n\t{1}".format(inputpath, outputpath))
reader = imageio.get_reader(inputpath)
fps = reader.get_meta_data()['fps']
writer = imageio.get_writer(outputpath, fps=fps)
for i,im in enumerate(reader):
sys.stdout.write("\rframe {0}".format(i))
sys.stdout.flush()
writer.append_data(im)
print("\r\nFinalizing...")
writer.close()
print("Done.")
for file in [x for x in os.listdir(video_dir) if x.endswith(".mp4")]:
convertFile(f"{video_dir}{file}", TargetFormat.GIF)

Download All Videos

for file in os.listdir(video_dir):
files.download(f"{video_dir}{file}")
Image for post
Image for post
Image by Author

Key Takeaways

Other Posts By Dylan

Resources

Robot Academy

Automate the things that you don’t want to spend time doing.

Dylan Roy

Written by

Dylan Roy

Engineer & Side Project Junkie sharing what I am working on so readers like you can benefit from my experiences. Subscribe here for even more (dylanroy.com)

Robot Academy

From posting twitter messages from Slack to finding the best deals on Amazon. We help you learn to start automating, and to grow your army of robot minions now!

Dylan Roy

Written by

Dylan Roy

Engineer & Side Project Junkie sharing what I am working on so readers like you can benefit from my experiences. Subscribe here for even more (dylanroy.com)

Robot Academy

From posting twitter messages from Slack to finding the best deals on Amazon. We help you learn to start automating, and to grow your army of robot minions now!

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store