Press enter to skip the top menu

Computer Graphics CS

Translation

Download Program Code

LearningOutcomes

On completion of this lesson you will know how to create simple animation by making separate images and moving a shape slightly in each image.

Go to top

Introduction

By now we have looked at making static images by both drawing their outlines and filling in their shapes. In this lesson we shall be making the images move. In other words we shall be dealing with animation. Your first reaction may be that there will be very complex programming involved. This is not the case. We shall not even be adding a function to our code. Instead we shall slightly modify the main() and saveFile() functions.

The lesson follows on directly from the previous one. We shall be filling in a rectangle as before and using the exact same functions to fill in the space of the rectangle.

What is going to be different is that instead of creating one file we shall be creating a number of separate .PBM files. In each file we position the image in a slightly different position to the previous one. When viewed together quickly the the shape appears to move. This is the basis of all animation.

Go to top

The program controller

Listing 1: the function main()
                        def main():
                            arrPage = []
                            strFileName="Translate"
                            intVersionNumber = 0
                            intMaxCols=800
                            intMaxRows=800
                            for frameCount in range(0, 680, 9):
                                arrPage = createBackground(intMaxCols,intMaxRows)
                                arrPage = fillShape(arrPage, [[250,frameCount],[250,frameCount+100],[550,frameCount+100],[550,frameCount]], intMaxRows, intMaxCols)
                                saveFile(arrPage,intMaxRows, intMaxCols,strFileName, intVersionNumber)
                                intVersionNumber+=1
                                arrPage=[]
                        
                        if __name__ == "__main__":
                            main()
                        
                        print("Programme finished")
                    

Although still a fairly short piece of code, a few changes have been made to this function. A new variable has been added and the entire processing is inside a for loop.

New variable intVersionNumber

The first change is at line 60 where we have added a new variable intVersionNumber. The reason for this is that we shall be creating multiple version of our PBM file. To distinguish each individual file we shall add a version number to the file name.

Other changes

The major change is the processing which spans lines 63 to 68. The processing is controlled by the for loop at line 63. The first thing that needs explanation is the controls of the loop itself, especially the range values of (0, 680, 9).

The loop controls

Unless stated otherwise for loops start zero and this is the case here. The value 680 is somewhat more complicated.

Instead of placing our shape vertically in the middle of the image, in this instance we want to place it at the very top, i.e. it's top y values will be at zero. Also since it is 100 pixels high, its lower y values will be at 100. This means that its four coordinates will be (250, 0), (250, 100), (550, 100), (550, 0).

The values 680 and 9 must be explained together as they are interrelated. Clearly 680 is the upper limit for the loop counter frameCount and 9 is the amount that frameCount increases for each iteration of the loop.

We want a five second animation at a frame rate of 15 frames per second. During that five seconds we want our rectangle to move as close to the bottom the image as we can. This means that we want 75 frames.

If in each frame the rectangle moves down 9 pixels from the top then by the last frame the top of the rectangle will be at y position of 675. This means that the bottom of the rectangle will be at y position 775, which is as far as we can go.

At line 65 the function fillShape() is called. This time we shall look in detail at the list argument

[[250,frameCount],[250,frameCount+100],[550,frameCount+100],[550,frameCount]]

At the first iteration of the loop with frameCount having a value of zero this list will produce the coordinates
(250, 0), (250, 100), (550, 100), (550, 0)

With frameCount increasing by 9 for each iteration of the loop, other coordinates will be
(250, 9), (250, 109), (550, 109), (550, 9)
(250, 18), (250, 118), (550, 118), (550, 18)
(250, 27), (250, 127), (550, 127), (550, 27)
(250, 36), (250, 136), (550, 136), (550, 36)

Changes to how the function saveFile() is called

At line 66 the function saveFile() is called. This time it has an extra agrument, intVersionNumber. We shall later look at this function in more detail.

Tidying up

After the function saveFile() finishes control returns to line 67. By now the contents of a frame has been saved to a PBM file and all that remains to be done is to tidy up for the next file.

At line 67 the value of intVersionNumber is incremented by 1 so that the next frame to be saved has a unique name.

The two dimensional list arrPage still has the contents of the previous frame in it and thus we must initialise it to a blank. This is done at line 68.

Go to top

Saving the file()

Listing 2: the function saveFile()
                        def saveFile(arrAll, maxRows, maxCols,fileName,versionNum):
                            myfile=open(fileName+str(versionNum)+".pbm",'w')
                            myfile.write('P1' +"\n")
                            myfile.write(str(maxRows)+" "+str(maxCols)+"\n")
                            for intRows in range(maxRows):
                                myfile.write(getArray(arrAll[intRows])+"\n")
                            myfile.close()
                    

Of this file only lines 43 and 44 have changed. The reason for those changes is that unlike our previous examples our PBM files are now individual frames in an animation sequence. Therefore as well as a file name, which identifies the entire animation sequence we need a number to identify the individual frames, hence the new argument versionNum at line 43.

At line 44 the file name is built up from its constituent components. Thus if the argument fileName has a value of 'Translate' and versionNum has a value of 2, then the full name of the file will be 'Translate2.pbm.

names of files in an Explorer window
Fig 1

Here we see the first twenty frames from which we are to create our animation.

Go to top

Converting PBM files to a GIF image

Although PBM files can be opened with GIMP or PhotoShop they cannot be opened by image viewing software or HTML. So that these other software can use them they must be converted first to standard file formats such as JPG or PNG.

This process is very simple. First you open the PBM files in the same way as any other file and then use either Save As or Export As. As an extension specify either JPG or PNG.

In out current example we have 76 PBM files that we wish to convert to an animated GIF file. For this exercise we shall be using the open source image processing software GIMP.

With GIMP running click on File/Open as Layers... This will bring up the Open File dialogue box shown below

an open file dialogue box
Fig 2

To begin with select the first of the files you want to open. Next hold down the SHIFT key and then click on the last file in the list you want to open. This selects all of the files between the two you clicked on. The files may take a while to load but once complete you will have 76 layers, one for each file you uploaded. Some of the layers can be seen below in Fig 3.

By stopping here we have enough files to create an animation. It will however appear somewhat amateurish. The animation will begin at the top and move gradually down to the bottom. It will then jump back up to the top and start moving gently down to the bottom again.

To avoid this break the same set of files is opened once more but in reverse order. To reverse the order of the files select Open Image as Layers again and reverse the order of the files by clicking the upside down V next to the word 'Size'. Once this is done select from the last file to the first one and proceed as before.

part of the Layers box showing thumbnails of the layer contents
Fig 3

All you need to do now is click on File/Export As. This will bring up the export dialog box shown below.

selecting the file type
Fig 4

Supply a name for the file and from the Select File Type select GIF.

Clicking on the button Export brings up the dialog box below.

specifying we want animation
Fig 5

Here ensure that the checkbox labelled as As animation is checked. Now click on Export again, and your frames will be saved as a GIF image.

This image can now be opened by most file viewing applications, including HTML as shown below.

demonstration of simple animation
Go to top