python-course.eu

24. Moving Watermarks Video with Python

By Bernd Klein. Last modified: 26 Apr 2023.

The idea behind this chapter of our Python course is to create a film from one or more pictures where one or more object in form of watermarks are moved around.

We will move around solid objects or shining-through watermark objects. We created an image with a watermark structure for our chapter on decorators and decoration. You can see it close to the top of the page: An "ampersand" (better known in the Python community as the decortor sign. You can also find a course on how to create watermarked images like this with Python, Numpy, Scipy and Matplotlib in the chapter on Image Processing Techniques. It explains the whole process of the making-of of our decorator and at sign picture, which means watermarked images. So consulting these chapters before going on may be a good idea. Technical note: opencv

Creating Interesting Objects with Matplotlib

We start by creating some nice objects which we can use in the film we want to create as moving objects. We start by creating a directory to hold the objects:

import os
import shutil
target_dir = 'images4video'
watermarks_dir = f"{target_dir}/watermarks_dir"
if os.path.exists(watermarks_dir):
    # delete the existing directory
    shutil.rmtree(watermarks_dir)
# Create target_dir, because it doesn't exist so far
os.makedirs(watermarks_dir)

We use in the following code the parameter bbox_inches="tight" in savefig. When this parameter is set to "tight", matplotlib will try to remove any extra whitespace or padding around the edges of the figure, so that only the actual content of the figure is saved. This can be useful when you want to save a figure with minimal margins or padding.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 1000)
y1 = np.sqrt(1-(abs(x)-1)**2)
y2 = -3 * np.sqrt(1-(abs(x)/2)**0.5)

fig, ax = plt.subplots()

ax.fill_between(x, y1, color='red')
ax.fill_between(x, y2, color='red')
ax.axis(xmin=-2.3, xmax=2.3)
ax.axis('off')
#ax.xlim([-2.5, 2.5])
txt2include = "python-course.eu"
ax.text(0, -0.4,
         txt2include, 
         fontsize=24, 
         fontweight='bold',
         color='orange', 
         horizontalalignment='center')

plt.savefig(f"{watermarks_dir}/heart.png", bbox_inches="tight")

moving_watermarks_video: Graph 0

We create now a an image with a black and white spiral:

import numpy as np
import matplotlib.pyplot as plt

theta = np.radians(np.linspace(0, 360*5, 1000))
r =