030 - Loading Images

Helmut  Knaust - Department of Mathematical Sciences, University of Texas at El Paso, hknaust@utep.edu
08/30/2010; revised:08/27/2015; last edit 08/31/2020

Initial Setup

A link to sample images called “Photo Set” can be found in ZIP format on the class website.

Download this zip file and unzip it into the root directory on your USB stick.

The following command assumes that the notebook "DW.m" is in the same directory this notebook file is in.

SetDirectory[NotebookDirectory[]]
<<DW.m

Loading an image

Drag and drop an image into the notebook:

Give it a name:

pic=

“pic” is the name of the photo. We will need the underlying matrix! We will call it “dat”. Just copy the next command every time you want to convert a photo to a matrix.
The “;” at the end suppresses the output (=huge matrix)

dat=ImageData[pic,"Byte"];

Here is a small piece of the matrix:

dat2=dat[[12;;14,22;;26]]

To see the photo representing the matrix, use “ImagePlot”.

ImagePlot[dat]

ImagePlot[dat2,PixelConstrained->60,Scaled->False]

Alternative Method: Loading a black and white image

The following command assumes that the directory "Images" is in the same directory this notebook file is in.

SetDirectory["Images"]

Directory[]

FileNames[]

SetDirectory["Grayscale"]

FileNames[]

pic="chess.png";

The image is represented by a large matrix with entries between 0 (Black) and 255 (White):

data=Import[pic,"Data"];
data2=data[[12;;24,12;;20]]

ImagePlot[data,PixelConstrained->2]

The upperleft corner of the image as a matrix :

data[[1;;10,1;;10]]

ImagePlot[data2,PixelConstrained->40,Scaled->False]

In our case the matrix/image has 512 rows and 768 columns (=393,216 entries) :

dim=Dimensions[data]

Since each entry requires 8 bit of information (Integer between 0 and 255), the total storage requirement in raw form is 512 x 768 x8 =3,145,728 bits
 (≈393 KB) :

512 768 8

Alternative Method: Loading a color image

SetDirectory[NotebookDirectory[]]

SetDirectory["Images"]
FileNames[]
SetDirectory["Color"]
FileNames[]

pic="facade.png";

A color image is represented by a large matrix with "triple" entries between 0  and 255 :

data=Import[pic,"Data"];

ImagePlot[data]

dim=Dimensions[data]

The lowerleft corner of the image as a matrix :

ll=data[[503;;512,1;;10,All]]

Since each entry requires 8 bit of information (Integer between 0 and 255), the total storage requirement in raw form is 512 x 768 x 3 x 8 = 9,437,184 bits 
(≈1.18 MB):

512 768 3 8