Menu

Executive Programs

Workshops

Projects

Blogs

Careers

Student Reviews


For Business / Universities

Corporate Training

Hire from US

Academic Up-skilling



All Courses

Choose a category

Loading...

All Courses

All Courses

logo

Mechanical

Uploaded on

26 Nov 2022

How To Stitch Images In Sequence To Get Smooth Animation?

logo

Skill-Lync

We can use the PIL (or Pillow) module in Python to generate an animation out of the.png images we got from executing the second-order ODE algorithm. We may import the Image module from the library. A class with the same name is provided by the Image module and is used to represent a PIL image. The module also offers a number of factory functions, including tools for generating new images and loading images from files.

Additionally, we need to use Python's built-in glob function.  The glob module uses the Unix shell's rules to identify all pathnames that match a given pattern, however, the order in which the results are returned is random.

Now, you can stitch the images together with the help of these libraries and functions in the following manner:

Method 1: Modify the code in VSCode as shown below

Before you start making any changes in the code, you need to install a couple of libraries, i.e., natsort and pillow.

Step 1 - On installing the two libraries, you need to import them in your code. These are all the libraries you need to import.

Step 2 - Now, to get all the .png files from your working directory, we make use of the glob.glob() command, as shown below. We need to create a variable called "imgs" to include all the .png files in that variable.

You can also make use of a print statement to display how to glob function gets the .png files and when you do that, you will observe that these files are taken at random, and in no specific order, as shown in the screenshot below:

It’s evident from the terminal (in VSCode) that the image names are jumbled and not in any specific order. To sort the whole image array in ascending order, we use a python library called natsort.

Step 3 - imgs is the array of images which are to be sorted.

reverse=False will arrange all the images in ascending order.

The print statement here displays the .png files in ascending order, as shown in the snippet below.

Step 4 - If we want to create an animated GIF, we need a group of images that make up the individual frames.

frames=[] is a list of opened Pillow Image objects, here is how you would save it:

frames=[] is an empty array in which we will be adding separate images by using the Image module from the PIL.

The for loop is used to call the Image module from PIL to open each image in the current folder/ directory.

Image.open() opens and identifies the given image file.

frames.append(new_frame) allows the opened image to be appended into the empty array ‘frames’.

Step 5 - Once all the images are appended to the empty array named 'new_frame', now we can start creating the animation for the images stored in the frames array.

frames[0] tells the compiler to start the animation from the 0th indexed .png image.

save() is the command used to save the images in form of a GIF.

‘Pendulum_Animation_using_Python.gif’ is the name of the GIF you’re about to save.

format=’GIF’ will specify the format of the intended file.

append_images is a list of the images to save.

Since the first image (images[0]) is used to make the call, the extra images consist of images[1] and beyond. We use a slice to select those images from the full list.

save_all tells Pillow that we want to save multiple images in the same output file.

duration controls how long each frame will be displayed for, in milliseconds. It is the inverse of the frame rate.

Setting it to 100 milliseconds gives 10 frames per second. You can decrease the duration to give a smoother animation.

loop controls how many times the animation repeats.

The default, 1, means that the animation will play once and then stop (displaying the last frame). 2 means it will repeat twice before stopping, and so on. A value of 0 means that the animation will repeat forever.

Method 2: Modify the code in Octave as shown below:

This command will save the file name starting with zero and will have 5 digits. Since we are increasing the value of “ct” by 1 in every loop, each frame will get a unique name and will be in sequence like shown below,

Now that all the frames are arranged sequentially, they will be stitched sequentially to generate a smooth animation, with the help of third-party software called ImageMagick. This software will be used in the command window.

The command to stitch all frames (i.e. .png files) in the command window is as follows:

magick *.png Pendulum_animation.gif

*.png command makes sure that ALL .png files are tsken into consideration (* means ALL)

Pendulum_animation.gif -  It is the name given to the GIF file which will be created. You can give any name you'd like.

Remember to add the extension .gif after giving a name to the animation.


Author

author

Navin Baskar


Author

blogdetails

Skill-Lync

img

Continue Reading

Related Blogs

Moving Frame of Reference

A Moving Reference Frame (MRF) is a very straightforward, reliable, and effective steady-state Computational Fluid Dynamics (CFD) modeling tool to simulate rotating machinery. A quadcopter's rotors, for instance, can be modeled using MRFs.

Mechanical

12 May 2023


Analysis Settings in Ansys Software

Analysis settings in Ansys are the parameters which determine how the simulation should run.

Mechanical

08 May 2023


Comparing the Explicit and Implicit Methods in FEA

In Ansys, the analysis settings play a very important role in converging the solution and obtaining the results. These involve settings about the timestep size, solver type, energy stabilization etc.

Mechanical

06 May 2023


Tensors, Stress, and 2D Meshing: A Primer for Beginners

A tensor is a mathematical object that describes a geometric relationship between vectors, scalars, and other tensors. They describe physical quantities with both magnitude and direction, such as velocity, force, and stress.

Mechanical

05 May 2023


Reynold's law of Similarity

The Reynolds number represents the ratio of inertial to viscous forces and is a convenient parameter for predicting whether a flow condition will be laminar or turbulent. It is defined as the product of the characteristic length and the characteristic velocity divided by the kinematic viscosity.

Mechanical

04 May 2023



Author

blogdetails

Skill-Lync

img

Continue Reading

Related Blogs

Moving Frame of Reference

A Moving Reference Frame (MRF) is a very straightforward, reliable, and effective steady-state Computational Fluid Dynamics (CFD) modeling tool to simulate rotating machinery. A quadcopter's rotors, for instance, can be modeled using MRFs.

Mechanical

12 May 2023


Analysis Settings in Ansys Software

Analysis settings in Ansys are the parameters which determine how the simulation should run.

Mechanical

08 May 2023


Comparing the Explicit and Implicit Methods in FEA

In Ansys, the analysis settings play a very important role in converging the solution and obtaining the results. These involve settings about the timestep size, solver type, energy stabilization etc.

Mechanical

06 May 2023


Tensors, Stress, and 2D Meshing: A Primer for Beginners

A tensor is a mathematical object that describes a geometric relationship between vectors, scalars, and other tensors. They describe physical quantities with both magnitude and direction, such as velocity, force, and stress.

Mechanical

05 May 2023


Reynold's law of Similarity

The Reynolds number represents the ratio of inertial to viscous forces and is a convenient parameter for predicting whether a flow condition will be laminar or turbulent. It is defined as the product of the characteristic length and the characteristic velocity divided by the kinematic viscosity.

Mechanical

04 May 2023


Book a Free Demo, now!

Related Courses

https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/vehicle-dynamics-matlab_1636606203.png
Vehicle Dynamics using MATLAB
4.8
37 Hours of content
Cae Domain
Know more
https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/introduction-control-electric-vehicle_1612329773.png
4.9
16 Hours of content
Electrical Domain
https://d28ljev2bhqcfz.cloudfront.net/mainproject/thumb/meshing-and-connections-deployment-of-rear-suspension-of-a-car-73676_1616585003.jpgRecently launched
0 Hours of content
Cae Domain
Showing 1 of 6 courses
Try our top engineering courses, projects & workshops today!Book a Live Demo