Timothy Madegwa
4 min readOct 11, 2019

--

BASIC IMAGE EDITING WITH PYTHON

Hello there, Its been a while since my last post so today I’ve decided to write a simple post on how your edit/manipulate an image using Python. In this post, I am going to use a popular python library called Matplotlib to do some basic image manipulation.

REQUIREMENTS

  1. Python 3.6 or later (I used Python 3.6.4 for this post)
  2. Matplotlib 2.1.2
  3. Jupyter notebook

You do know how to check your Python and Matplotlib versions? Well don’t worry, I got you. To check your version of python, open your command prompt or terminal and write this command python - -version.

For Matplotlib:

  1. open your terminal or command prompt
  2. type in the ‘python’ command
  3. type in ‘import matplotlib’ and press enter
  4. type in ‘matplotlib.__version__’ and press enter
checking the Python and Matplotlib versions

We start off by importing Matplotlib.pyplot with its alias plt, and also as an alternative way of importing our image, we can also import matplotlib.image as mpimg. Having done that, we can now load our image using pyplot’s imread() function as shown in figure 1 below. Matplotlib is built on numpy and so any image loaded through it is stored as a numpy array.

figure 1

From figure 1, we can see that the image is a 3-D numpy array with 1926 rows and 2694 columns. The other 3 at the end is just the RGB values for the image. Here, the image is stored with values ranging from 0 to 255, representing the various RGB values.

Let’s have a look at the image using pyplot’s imshow method.

Figure 2

Now, I can apply various color maps(filters) on the image and see how it affects the image.

Figure 3

I can also collapse the image by taking the average of the RGB values and apply the color maps(filters) on the same image as show in the code below.

Figure 4
Figure 5

Here is a list of the different color map values you can use to edit your picture.

[Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r, Vega10, Vega10_r, Vega20, Vega20_r, Vega20b, Vega20b_r, Vega20c, Vega20c_r, Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spectral, spectral_r, spring, spring_r, summer, summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, tab20c_r, terrain, terrain_r, viridis, viridis_r, winter, winter_r]

I can also compress the image by taking every 3rd pixel of the image as shown in the figure below. I’ll start by checking the shape of the image(pixels) before and after compressing. From that I can see that the shape chaged from (1926,2694) to (642,898).

Figure 6

Finally I can save my compressed and filtered image using the code below.

Figure 7.

I hope you have learnt something from this post and you can now edit your own photo using Matplotlib.

For advanced image processing, I found an awesome article that you can read here.

--

--