What is a digital image? In this video, we will discuss what is a Digital Image. Processing digital images in Python. A digital image can be interpreted as a rectangular array of numbers. In many cases it's easier to understand a gray-scale image, an image that is made up of different shades of gray. If we zoom into the region, we see the image is comprised of a rectangular grid of blocks called pixels. We can represent these pixels with numbers called intensity values. We overlay the intensity values over the pixels. In the real world, an image can take on an almost unlimited number of values, but digital images have intensity values between zero and 255. It turns out that's all we need, 256 different intensity values to represent an image. The following bar demonstrates the relationship between the different shades of gray and the numerical values. Darker shades of gray have lower values with zero as black and lighter shades have higher values with 255 as white. The contrast is the difference between these values. Let's see what happened if we use less values. Consider the following image, on the left, we have 256 intensity values. On the right, we have 32. The images look similar. Let's reduce the number of intensity values on the right image. Here we have 16 intensity values. You can start seeing a difference in regions of low contrast. Here we have eight intensity values and the image looks off. Here we have two intensity values. The image looks like a cartoon. At the end we have an array of numbers, the height is the number of rows, in this case 500 as shown in the plot, and width is the number of columns, in this case 500. It's like a rectangle or in this case, a square. Each pixel or intensity value has its own index. For rows, we start at the top of the image and move down. For columns, we start at the left of the image and move right. Each pixel value comes from a grid of sensors. We have the original object. The image is the quantized samples obtained from the grid. Consider the following image, it's a combination of red, blue and green images, it's sometimes referred to as an RGB image. This is just one of many color representations. These color values are represented as different channels. Like the gray-scale image, each channel is an image. We have the red channels intensity values, the blue channels intensity values and the green channels. If a gray-scale image is a square, a color image is like a cube. Each one of these channels has its own intensity values. In addition to accessing each image intensity with a row and column index, we also have an index for each channel, in this case, zero for red, one for blue and two for green. We can have black and white images. Here we have an image mask used to identify objects. The intensities corresponding to the person are represented with one and the rest are zeros. The display adjusts contrast, so zero is black and one is white. The video sequence is a sequence of images. Here we have five images representing five video frames. For the first frame we have, for the second frame we have, and so on. An image is a file on your computer. Two popular image formats, Joint Photographic Expert Group image or JPEG, and Portable Network Graphics or PNG, these formats reduce file size and have other features. No matter what Python library you use, you're going to have to load the image. If your code is in the same directory, all you need is the name of the file. If your code is in a different directory, you will need the path of the file. The pillow, or PIL library is a popular library for working with images in Python. We will use it for some tasks. We import the image module from PIL, you can load the image as follows and create a PIL image object. You can plot the image using show but we will use matplotlibs imshow. The attribute format is the extension or format of the image. The attribute size is the number of pixels that make up the width and height. The attribute mode is the color space, in this case, RGB. ImageOps module contains several ready-made image processing operations. We can convert the image to gray-scale as follows, the image mode is L. This means luminance. We can save the gray-scale image and we can convert it from PNG to JPEG. We can load gray-scale images. The image mode is L. We can use the quantize method to quantize the image. The input is the number of quantization levels. You can also work with the different color channels. Consider the following image, we can obtain the different RGB channels as gray-scale images and assign them to the variables red, green and blue using the method split. We can plot the Red Channel as a gray-scale image. We can see that the red pixels and the gray-scale plot of the red have a higher intensity. We can do the same for the blue channel. The relationship is the same. There are several ways to convert a PIL image to a numpy array. For example, we can use the array constructor, we can print the array. These arrays are similar to the OpenCV arrays that we will review in the next section. Check out the lab for more. OpenCV is a library used for computer vision. It has more functionality than the PIL library, but is more difficult to use. This will be our main focus. We can import OpenCV as follows, the imread method loads an image from the specified file. The input is the path of the image. The result is a numpy array with intensity values as 8-bit, unassigned data types. We can obtain the shape of the array. We can plot the image using Imshow, but the colors appear off. This is because the order of each channel is different in OpenCV unlike PIL that is RGB. OpenCV is BGR. This is the main difference between the arrays and PIL versus OpenCV. We can change the color space with conversion code, this changes the color space. We use the function cvtColor, the input is the color image and the color code BGR to RGB or blue, green, red to red, green, blue. We can now plot the image. You can also convert the image to gray-scale using cvtColor. The input is the original image and the BGR to gray color code. We can plot the image. We can save the image using imright, the input is the path and the image array. We can also load gray-scale images in OpenCV. We must specify the color code for imread color scale. We can load a color image and use slices to obtain the different color channels. We assign each channel to the corresponding array, blue, green and red. Slicing selects each row and column. The final index selects the color channel. We can view the color channels blue, green and red.