How To Convert Image To Matrix Using Python

In this tutorial, we are going to learn how to convert an image to the matrix in Python. Before we get into our problem, basic ideas should be made clear to all.

What is Image Processing In Python

  • Image Processing in Python is a technique or method through which data of Image can be retrieved in the form of numbers.
  • This is done so because at last, the work we want through the process will be executed with the computers.
  • The libraries which are commonly used for this are NUMPY, MATPLOTLIB  and PILLOW.

How to implement the Image Processing Technique to our motive

  • As we all know that there are various libraries and modules which can be integrated with Python.
  • Here, We will be using PILLOW and NUMPY because these libraries are easier to understand and less sophisticated.

Convert Image To Matrix in Python

  • Import Image module from PILLOW library of Python as PIL.
  • Import array module from NUMPY library of Python.
  • These two libraries are for Image extraction from the source file and defining the dimensions of the matrix.

 

Now, let us code to implement it.

from PIL import Image
from numpy import array
im_1 = Image.open(r"C:\Users\CHITRANSH PANT\Desktop\New Chrome Logo.jpg")
ar = array(im_1)
ar

The output from the above code, as follows.

array([[[146, 166, 177], [177, 197, 208], [143, 163, 174], …, [177, 197, 208], [146, 166, 177], [176, 196, 207]], [[176, 196, 207], [178, 198, 209], [176, 196, 207], …, [175, 195, 206], [170, 190, 201], [168, 188, 199]], [[142, 162, 173], [177, 197, 208], [143, 163, 174], …, [177, 197, 208], [142, 162, 173], [176, 196, 207]], …, [[176, 196, 207], [176, 196, 207], [173, 193, 204], …, [176, 196, 207], [177, 197, 208], [173, 193, 204]], [[138, 158, 169], [171, 191, 202], [150, 170, 181], …, [173, 193, 204], [145, 165, 176], [176, 196, 207]], [[177, 197, 208], [179, 199, 210], [176, 196, 207], …, [166, 186, 197], [172, 192, 203], [173, 193, 204]]], dtype=uint8)

Here I am providing you all with the Image so that you can take it as your example.

Convert Image To Matrix Using Python

Also read:

One response to “How To Convert Image To Matrix Using Python”

  1. chaitanya says:

    Could explain better. I putted it in google colab it is not working.its showing error

Leave a Reply

Your email address will not be published. Required fields are marked *