Creating your own .gifs! Part 1

Ok this week we are going to start something fun! Gifs are the new fun way to express:  how people feel, a situation, or just for fun.  But gifs can be used for your science and math also! Say you have a presentation or a project where you would like to show the changes to your graph in a rolling images rather then a mosaic of images. Well you can do this in R! I have found two easy ways of doing this. Both are relatively easy to do, however you have to do a bit of leg work before you get to the fun part.  When I was researching this for a project I was working on, I found that the most important part was downloading the packages needed to execute the .gifs. However in most tutorials out there would glaze over this part, as if you should already know how to download the packages needed. Since they aren't as strait forward as we would like, I will go over that first then show you how you can make a fun easy .gif for your work. I will brake up this blog into two parts to cover the two methods you can use.
Part 1: Making a .gif using R and ImageJ.
Part 2: Making a .gif using R and ImageMagic. (Next blog)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Part 1: Making a .gif using R and ImageJ. 

At first I couldn't figure out how to use some of the image packages out there I decided to use an old friend. ImageJ. ImageJ is a great tool as I am sure most of you know. Its free and can be easy to use. So for this section make sure you have ImageJ downloaded.

Download ImageJ

Note I have an older version of ImageJ it can be downloaded from Here (this isn't the only place you can get this software).

The download for this app is really strait forward. Download the correct for your operating system zip file and open up and run the .exe file. This will prompt a popup window. It will ask you to extract all or run. Choose the Extract all and save the files in a folder that you would like this program to be in (Note don't just save it in the downloads folder). Once its unpacked then you can choose the appropriate .exe for your computer. Note that they have both the old and the new version available to use.  Ok now you should have both R and ImageJ up and running on your computer. Now we can start on the fun!

What is behind a gif?

As you should know to make any movie or animated object you have to understand the basics behind it. Movies and gifs are really rapid succession of several photos. The number of photos that make up an animation depends on your succession rate and the total time. Movies generally have a liner timeline. Once you get to the last image in the video the video stops. Where as gifs are more of a short video that is placed on a continuous loop. Meaning once the animation gets to the last image it starts over with the first image and this continues on indefinitely.  So why am I telling you this? Well in order to make a gif you will have to make several images that move in a linearly through time.

Creating Images using R

So we will make all of our images using R. The great thing about R is that you don't have to download a new library package to save an image. You just have to do a few things to make sure that they save how you need them too. So lets get started!

The first thing I recommend doing is setting a directory. That way all your images will be saved to a specific folder that you created for this project. Its also smart to make a brand new folder to save all the images in.

setwd('C:/Users/Desktop/GifProject/')

Once you have your directory set you can start creating your images. If there are several you would like to make a loop is probably the easiest way to make it. That way you can automate the file name and automate the number of images you would like to make. For my example, I am going to create a pie chart that spins. Not to fancy but fun. Note you can do this with almost any kind of image. The code to make those images is below:

# this creates equal seized wedges for out chart
slices <- rep(25,20) 

# Number of frames we will need to make a smooth continuous gif (since its a circle and I rate will be 10 degrees at a time I will need 36)
frames = 36 # we need a full circle rotation so we need 36 photos
Start = 0 # Start the circle starting at 0 degrees
  
  #Loop to create and print the images
  for(i in 1:frames){ 
    # creating a name for each plot file with leading zeros
    if (i < 10) {name = paste('000',i,'plot.png',sep='')} 
    if (i < 100 && i >= 10) {name = paste('00',i,'plot.png',sep='')}
    if (i >= 100) {name = paste('0', i,'plot.png', sep='')}
    
    # Name and print the image
    png(name,width=3.24, height=4.5,units = "in",res=72)
    # Give it a black background
    op <- par(bg = "black")
    
    # pie graph
    pie(slices,labels = NA, col=c("Black","White"),
          init.angle = Start) # Plot the pie graph 


    dev.off() # Says that is the end of making the image
    
    Start = Start-10 # changes the angle of the next graph by 10 degrees going clockwise

  }

So now if you have run this code you will have 36 images in the folder you created. I would check to make sure they are doing what you want before we move onto the the ImageJ. 

Making a .gif using ImageJ

Once you are happy with the images you have created its time to make the .gif or a video. We will do this in ImageJ. So open ImageJ. Then to upload the images we just created: 

Go to File --> Import --> Image Sequence… 

Navigate to the folder that has all the images in it. Then click on the first image in the sequence. It will have a number that looks like this: 0001plot.png 

Next a popup window will open. Make sure the following are done: 

Number of images: 36 # Note for the stripes make sure this number matches the number of images you have in the folder. 
Starting image:
Increment: 1
Scale images: 100% 
Convert to RGB: is checked 
Sort names numerically: is checked
Once all of the above are satisfied press the OK button. 

This should upload your images into a single window. You can press the play button to see your images in action as a sequence. To adjust the speed of the sequence: 

For the old version of ImageJ: Go to Images  --> Stacks  --> Tools  --> Animation options… 
For the new version of ImageJ: Go to Images  --> Stacks  --> Animation  --> Animation options…

A new popup window will open. You can change the speed in the Speed box. This is number of frames per second. So the smaller the number the slower it goes and visa versa. Then press OK. 

Once you are satisfied with your image you can save it in two formats. First format is a video format.

 Go to File  --> Save As  --> AVI… 

There will be a pop up box where you can adjust the compression and frame rate. Compression: JPEG Frame Rate: (Should be the same as your speed) Then press OK. Then the save window will pop up. Rename and save your movie. 

To save your images as a .gif file. First you need to change the image format: 

Go to Image  --> Type..  --> 8-bit color 

Then save the file: 

Go to File  --> Save As  --> Gif… 

Then press OK. Then the save window will pop up. Rename and save your .gif. 

If you followed along with my example you should have something like this: 


So start having fun with making gifs! If you want to learn how to make them using only R. See next blog: 

Part 2: Making a .gif using R and ImageMagic. 






Comments

Popular posts from this blog

Women Who Code Connect!

Open Source Platforms ... Why aren't you using them yet?