Python Tutorial — Using Python to Create Simple Animations — Part 3

Subhayan Dutta
Wenable
Published in
4 min readApr 12, 2020

Introduction — Simple Animation with Rectangles

In the first and second part of this series of creating simple animations using Python, we looked at how to create simple animation using images, use 2 different images to highlight certain words and parts of images, and also drew simple lines on those images and created animations out of them. We also were posed a problem on how to split a line but still maintain the impression of the line(s) being drawn etc. In this part of the Python tutorial series, we will use the principles learnt in the previous tutorial and use that information to help us draw a square/rectangle around a certain image/text etc, as shown below. And of course we will solve the problem that was posted in the previous article.

Simple Animation Solution to Last Problem

Now, about the solution of the problem posted here, let’s try to understand what we need to do first. We need to have 4 different shorter lines and have the animations done for all the lines. To do that what you have to do is make sure we have 4 separate set of co-ords per line. Once that is identified, we need to make sure that the image modified after completing the fist part (of the four parts), and start the second part of the first line. We also have to make sure that from the second part of line onwards, you need to make sure that you need to copy the contents of the previous image, otherwise your changes on the first part of the image will be overwritten. But you already know that, right ? Good, lets continue with the problem discussed in today’s topic. Just in case, that you are stuck somewhere, do check in the solution posted here.

Here, by now, you already know how to draw a straight line and animate it. You also should be familiar with the environment that we will use to execute our scripts. As usual, we will use Docker environment to set up our execution environment which is discussed here. However we are not going to talk about the environment and just assume that you have been able to set the same up.

Coming back to the problem, what you need to know is that you need to draw a straight line as we discussed before, from left to right, a vertical line, from top to bottom, a horizontal line from right to left (opposite direction to the previous horizontal line and finally, a vertical line from bottom to top. Not too difficult, eh ?

The code for first horizontal and first vertical line appears as:

x1 = 120
y1 = 180
x3 = 880
for i in range(x1, x3, 40):
draw = ImageDraw.Draw(im)
x4 = x1 + filenamecounter*40
draw.line([x1, y1, x4, y1], width=5, fill=RED)
outImage = outdirectory + "Image" + '{:03d}'.format(filenamecounter) + ".png"
# im.show()
im.save(outImage)
filenamecounter += 1
x1 = 880
y1 = 180
y3 = 580
anothercounter = 1
im = Image.open(outImage)
for i in range(y1, y3, 40):
draw = ImageDraw.Draw(im)
y4 = y1 + anothercounter*40
draw.line([x1, y1, x1, y4], width=5, fill=RED)
outImage = outdirectory + "Image" + '{:03d}'.format(filenamecounter) + ".png"
# im.show()
im.save(outImage)
filenamecounter += 1
anothercounter += 1

This code creates a tiny winy problem. Lets look at the right top corner to the last image generated by the above code to understand what the problem is:

As you can see here, the 2 lines seems not to have blended together completely to have a sharp edge. Instead it seems the edge is a little bit frayed. What we need to do in this case is either to slightly increase the length of the horizontal line or have the y — coords of the vertical line start from a point slightly to the left of the end x — coords of the horizontal line. We did the later like this:

x1 = 878

With this change, we see the modified image as:

Likewise, for the rest of the 3 corners as well we make adjustments and generate the images in the “edited” folder. Stitch the images from the edited folder using the Stitching exercise that we did here and generating the video should be easy by now.

The whole application code along with the image used to create the animation, appears here.

As an exercise you can try out the following. This will be reversing the direction of the animation as shown below. But since this exercise will be straightforward by now, we will not provide any solution for this.

Conclusion

This mechanism of adding simple animations was used in WeGuard videos that were published on WeGuard Youtube channel. WeGuard is a flagship Mobile Device Management solution from Wenable. To learn more about our product, contact us today by visiting https://calendly.com/weguard. You can also sign-up for Enterprise edition for a free 30-day trial at https://cloud.weguard.io.

The original article was posted here.

--

--