Loading...

How to create a GIF of the Earth using NASA API

Making a GIF of the revolving Earth with NASA API

In this blog, we shall look at how we can create a GIF file of the revolving earth using NASA’s images of the earth as accessed from the NASA API…

May 13, 2021    By Team YoungWonks *

How does one make a GIF of the earth? Given how GIFs continue to be a part of Internet culture, here’s an interesting project you may be interested in. The answer is: using NASA’s API. Don’t know what it means? Worry not for this blog shall take you through the steps so as to create a GIF of the revolving earth by talking to NASA’s API. Before we go any further let us look at a few key terms and concepts. 

 

API, NASA, GIF: Key terms and concepts

To begin with, let’s look at NASA and API. NASA stands for the National Aeronautics and Space Administration, an independent agency of the U.S. federal government that deals with the civilian space program, aeronautics and space research. 

Meanwhile, API stands for application programming interface, an interface that allows interactions between multiple software applications or mixed hardware-software intermediaries. In simple terms, an API is provided by someone who has data for people to be able to access it without giving them access to their complete server. So in the context of this blog, NASA has a lot of information on its servers but this is one part that it wants to be available to everyone and an API makes this possible. 

Now when one wants to connect to an API, one needs something called an API key; it is the authorization key to get data from an API; its aim is to avoid overuse of the API and people spamming it. It is like a password to get access to the API. Once you know the API and have the API key you can make an API request, which is basically a call made to the API. This then elicits a response which is the data we requested. In this case we shall receive image data as response and it is binary in nature. 

 

Before we get started on the project, let’s also look at what the term GIF means. GIF stands for Graphics Interchange Format. Like the JPEG or PNG file formats, the GIF format can be used to create still images, but it’s mainly used to make animated images. Note that they aren’t videos, have no sound and are more like a flipbook. So a GIF file can hold multiple images in it and it typically loads them in a sequence. It has been and continues to be a part of popular Internet culture. 

 

Requirements for the project

The requirements for this project are as shared in the image below. One needs to have a laptop/ computer / Raspberry Pi, internet connection and Python 3 installed. Modules used are nasapy, requests (to communicate with the NASA server and get images), datetime (already installed). Extensions include pygame and time (already installed, useful for adding delays between the images). You can refer to the image to see the respective commands you enter to install them. 

 

 

Using the NASA API to create a GIF of the Revolving Earth 

First step is to install dependencies as shown in the image below.  


 

Next step is to go to https://api.nasa.gov/, enter your information, sign up with NASA and save the key you get somewhere safe. (The presenter’s API key has been blacked out to avoid misuse). You will also see a URL to which you can make your API request. 

 

Now we create a new Python file, import the dependencies of nasapy and requests, followed by creating a variable called key, set its value to be the API key we received in the previous step. Then we initialize the NASA module. Now we run this and check it out.

 

 

It should not give us any errors. 

 

Now we let's look at an overview of the next steps to get the raw data from the API as shown below.

 

First we get the raw data from the API and to do this, we start by creating a variable called today in the yy-mm-dd format. Here we have chosen the date of 2021-04-22 as today but one can update it to a later date. Refer to the pic below to see how this is done. 

 

Now we initialize NASA’s Epic API which provides us the images; we then look for images of the date we have specified as shown below. 

 

 

We then print the data, we see a lot of this data. It is a dictionary of dictionaries; but here we are only looking for the image name as seen below. If we get this from each of the dictionaries, we get all the images of the earth that were captured by NASA on that date (2021-04-22). 

 

Now here’s an overview of the next series of steps where we print all the image names. 

 

 

Here we need to loop over the images and print all the image names. Replace the hyphens with a forward slash in the date format and for more information on the NASA API, visit https://api.nasa.gov/. After doing this, you will see 11 images that were taken on the date. 

 

Now we have to download each of these images. Let us look at the steps for this stage.

 

We start by using our requests library. We have a URL to which we make a get request since we want to get an image and this format is also defined in NASA’s documentation so you can copy it off the NASA website. Once we do this we get the response. 

 

We then need to store the image into a file so we open a file. Note here that we say wb, so that it’s writing in binary. We also label each image as image 1, 2, 3, 4 and so on. This will help us later on, so we use an index variable as well. Then we write the image data into the file and close it. This is important because the data won’t get saved unless we close it. Then you run it. 

 

Now you will see images come up on the left side of the editor. They will come up one by one. You can see the images as shown below.

 

Now we will make these images into a GIF. Those who know how to use PyGame can load these images into PyGame and run them really quick one after another to make their GIF. 

 

But for those who do not know how to use PyGame, here’s what you can do. You can use a simple, easy-to-use website called https://ezgif.com/; all we need to do is upload these images on the website and it will generate a GIF with them in response. One can also modify the GIF settings and add more delays before making it. 



 

 

Now you see the GIF of the revolving earth (as seen on 2021/04/22) made using the NASA API. 

 

 Watch the video below to see how one can use the NASA API to create a GIF of the revolving earth: 

*Contributors: Written by Vidya Prabhu; Lead image by: Leonel Cruz

This blog is presented to you by YoungWonks. The leading coding program for kids and teens.

YoungWonks offers instructor led one-on-one online classes and in-person classes with 4:1 student teacher ratio.

Sign up for a free trial class by filling out the form below:



By clicking the "Submit" button above, you agree to the privacy policy
help