Analyze JPEG File Size for Random Pictures Using Random Function in Mathematica

Tanvir Prince

  Open Access OPEN ACCESS  Peer Reviewed PEER-REVIEWED

Analyze JPEG File Size for Random Pictures Using Random Function in Mathematica

Tanvir Prince

Mathematics, Hostos Community College, City University of New York, USA


Using the random function in Mathematica (a mathematical software), random numbers between 0 and 1 were generated which in turn used to create random pictures. The random values represent the gray scale of the black and white pictures. Thus hundreds of pictures were created randomly and compressed using JPEG algorithm. Then a directory list containing the file size is created and imported in the Microsoft excel worksheet to analyze. All the code is given in the paper that is used so that it can be carried out by any interested reader. As a byproduct, this can be used as a classroom activity or as a project for any college level mathematics courses.

At a glance: Figures

Cite this article:

  • Prince, Tanvir. "Analyze JPEG File Size for Random Pictures Using Random Function in Mathematica." American Journal of Applied Mathematics and Statistics 2.4 (2014): 246-248.
  • Prince, T. (2014). Analyze JPEG File Size for Random Pictures Using Random Function in Mathematica. American Journal of Applied Mathematics and Statistics, 2(4), 246-248.
  • Prince, Tanvir. "Analyze JPEG File Size for Random Pictures Using Random Function in Mathematica." American Journal of Applied Mathematics and Statistics 2, no. 4 (2014): 246-248.

Import into BibTeX Import into EndNote Import into RefMan Import into RefWorks

1. Introduction

Nowadays images are everywhere- from newspapers to internet. Most of the images that we see in our everyday life go through some types of compression to save space and transmission time through internet. In actuality, an image nothing but consists of a lot of pixels. For a black and white pictures, each pixel contains a numerical value between 0 and 1 which represent its gray level – 0 being completely white and 1 being completely black. For a color picture, each pixel contains a vector of length three where the value represent the magnitude of red, green and blue. Here red, green and blue are the primary colors from which all the other colors can be created. We use mathematica software and its random function generators to create hundreds of random value between 0 and 1. Then these random numbers were used to create random pictures. For simplicity, we only created pictures of size 16 by 16. But this size can be changed easily by modifying the code in the appropriate places. Later we exported these random pictures using “JPEG” extension and created an excel file using their file size. At the end we explore this to analyze and make some conclusion about their behavior.

2. Image to Pixel

Let us look at the following pictures:

Figure 1. A color picture of a flower [1]

We can simply drag this picture in the mathematica [2] notepad to start explore its various features. One of the command which convert the pixels into its numerical value is the command:


This will usually generate a huge array of data of which only a little part is shown below (because of the space).


Here only about 15 pixel values are shown and each pixel value is enclosed in parenthesis. If the reader looks carefully then he can find that each parenthesis contains three values. These are the values of RGB (red, green and blue value of the pixel).

3. Going Backward

Now we will invert the process. In other words, we will start with numbers and will create images from it. To make it simple, we will only consider black and white picture of size 16 by 16. So each picture has 256 pixels. In real life, a typical picture usually have dimension in the range of hundreds to even thousands. For example, the picture in Figure 1 have the dimension 259 by 194. So the total number of pixel in that picture is 50,246. We avoid the big numbers here because it will take a long time to process the data and there is no additional benefit that we will gain from this. The interested readers can change the code in the appropriate places to change the size of the random pictures. We use the build in command “RandomReal” to generate the random numbers. To be more precise, we use:


The above command generate a random 16 by 16 matrix where each entry is between 0 and 1. This will converted to a random picture. The following command:


Creates a list of 100 random images. The readers can change the last entry of the command to change the number of random pictures. The output is shown below:

4. Exporting Pictures

Now it is time to export all these pictures into a folder where we will extract the file size of each one of them. Here we have several choice on the format of the saved pictures. We choose “JPEG” format since it is one of the most common type of image compression used in general. To learn more about image compression see [3, 4, 5]. For more technical reading see [6] where some of the algorithm and the mathematics behind the image compression can be found.

We use the following command to do this:


The command specify the full path of the folder where we want to save the pictures.

5. Exporting File Size to Excel

Now we use the “Directory Listing” to export the all the file size into an excel file where further analysis of the result can be done. Only a small part of the table is given below

Note that by default the date and time of the export is given in two different column of the excel file. The fourth column is the file size. The unit of the file size is Byte.

6. Analyze Result

We created a histogram of the file size and get the following figure:

The following 3D pie chart is more explanatory then the histogram:

From Figure 2 and Figure 3 above, it is clear that the distribution is almost uniform throughout.

7. Conclusion

The uniform distribution of the file size testify the true random nature of the random function generator of the Mathematica software. Of course, no software can guarantee an ideal randomness but the mathematica software get very close to the actual situation. The experiment can be repeated using other software like “maple” or “matlab” to compare the result with each other. We invite interested readers to perform the similar experiment using different software platform. This can be assigned as a short project to a college level mathematics class. As a bi-product, students will be familiarized with the use of mathematica software. To learn more about mathematica see [7, 8].


[1]  In Courtesy of
In article      
[2]  For more information about the software and to download the product the readers can visit
In article      
[3]  Image Compression. (2011). Image Compression: How Math Led to the JPEG2000 Standard. Retrieved from Image Compression: How Math Led to the JPEG2000 Standard. (2011).
In article      
[4]  Group, S. (n.d.). JPEG Compression: What it is - when to use it - and when not to. Retrieved from Retrieved from the university of Oslo website:
In article      
[5]  Rahman Z., J. D. (n.d.). Image enhancement, image quality, and noise, Photonic Devices and Algorithms for Computing., (pp. VII, Proc. SPIE 5907).
In article      
[6]  Prince, T., Franco, S., Salva, I., & Windolf, C. (2014). Mathematics Behind Image Compression. Journal of Student Research, 3(1), 46-62. Retrieved from
In article      
[7]  Purdue University. (n.d.). A Brief Introduction to Mathematica.
In article      
[8]  Wolfram Mathematica. (2013). Hands-on Start to Mathematica. Retrieved from Wolfram:
In article      
  • CiteULikeCiteULike
  • MendeleyMendeley
  • StumbleUponStumbleUpon
  • Add to DeliciousDelicious
  • FacebookFacebook
  • TwitterTwitter
  • LinkedInLinkedIn