# Sharing some programming knowledge.

0%

As we all known, images that we saw on the screen are the combination of lots of pixels. Every pixel has its own color and was recorded in computer by a string of binary numbers. Commonly, each pixel is represented by 24 bit (3 bytes) binary numbers, and each byte represents Red, Green, Blue respectively. So the value of each color is in range 0 ~ 255. Specifically, a pixel in python can be stored as a list: [255,0,254]. But it’s more convenient and efficient to use python library `imageio`. Once we have stored it, we can start to analysis it.

Here I will take Firefox icon, because it’s my most frequently used browser and I’m using it now to write this Jupyter notebook.

## Then, store it using `imageio`:

``<class 'imageio.core.util.Array'>``

## We can print to check what it looks like:

``````(341, 419, 3)
[[[ 0  9 52]
[ 0  9 52]
[ 0  9 52]
...
[ 1  8 52]
[ 1  8 52]
[ 1  8 52]]]
428637
71.33186122523254
0 255`````` From `photo_data.shape` we can see actually we can treat it as a rank 3 ndarray (or call it a three layered matrix). The first two numbers here are length and width, and the third number (i.e. 3) is for three layers: Red, Green and Blue. We can use the first 2 dimensions to locate pixels and use the last dimension to access its color value, which is a rank 1 ndarray.

## Now we can try to change the pixels. Assume we want to change the pixels with low color values:

``````[[[ True  True  True]
[ True  True  True]
[ True  True  True]
...
[ True  True  True]
[ True  True  True]
[ True  True  True]]]`````` It became darker, meanwhile it also became more contrast.

## we can also use slicing to change colors in a range, instead of using boolean method: ## In some cases, we may want to mask images:

``(341, 1) (1, 419)``
``(341, 419)`` ## We may also want to mask some particular colors, instead of geographical areas: And…blue truned black, as expected.

## Applications?

Above are just some basic operations using numpy to analyse images. In reality, like satellite image analysis, which can help detect wildfire or track burnt areas, is far much more complicated.

( PS: This notebook is basically my learning note in UCSanDiegoX course: Python for Data Science )