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.
photo_data = imageio.imread("./firefox.png")
(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
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.
# set a filter, it's an ndarray with the same size as photo_data, but it only consists of boolean 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.
photo_data[0:20, : , :] = [255,0,0]
total_rows, total_cols, total_layers = photo_data.shape
(341, 1) (1, 419)
center_row, center_col = total_rows / 2, total_cols / 2
photo_data = imageio.imread('./firefox.png')
And…blue truned black, as expected.
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 )