Convert Image to Array using Python

We need to convert an image to an array to use it for any kind of data science task where we need to understand the features of an image. Converting images to an array is as easy as converting text to numeric data. So, if you want to learn how to convert images to an array, then this article is for you. In this article, I will present a tutorial on how to convert an image to an array using Python.

How to Convert an Image to Array using Python?

Converting an image to an array is an important task to train a machine learning model based on the features of an image. We mainly use the NumPy library in Python to work with arrays so we can also use it to convert images to an array. Other than NumPy, we can also use the Keras library in Python for the same task.

So in the section below, I will take you through a tutorial on how to convert an image into an array by using the NumPy and Keras libraries in Python.

Convert Image to Array using Python

Converting an Image to Array using NumPy:

We can use NumPy to convert images to arrays, but it has no function to read images. So first we need to use the PIL library in Python to read an image. If you’ve never used it before, you can easily install it using the pip command:

  • pip install Pillow

Now here is how we can read an image by using the PIL library in Python:

from PIL import Image
image = Image.open('aman.png')

After reading the image, here is how we can convert it into an array by using the NumPy library in Python:

from numpy import asarray
data = asarray(image)
print(data)
[[[188 216 238]
  [188 216 238]
  [187 215 237]
  ...
  [203 219 234]
  [203 219 234]
  [203 219 234]]

 [[187 215 237]
  [188 216 238]
  [188 216 238]
  ...
  [203 219 234]
  [204 220 235]
  [204 220 235]]

 [[184 212 234]
  [186 214 236]
  [185 213 235]
  ...
  [205 221 236]
  [205 221 236]
  [205 221 236]]

 ...

 [[131 154 172]
  [132 155 173]
  [134 156 174]
  ...
  [123 136 145]
  [126 139 148]
  [129 142 151]]

 [[129 152 170]
  [130 153 172]
  [134 156 173]
  ...
  [123 136 145]
  [124 137 146]
  [127 140 149]]

 [[130 153 171]
  [131 154 172]
  [132 154 172]
  ...
  [122 135 144]
  [125 138 147]
  [128 141 150]]]

Converting an Image to Array using Keras:

We can use the Keras library in Python both for reading images and converting them to arrays. So here is how we can read and convert an image to an array using the Keras library in Python:

[[[188. 216. 238.]
  [188. 216. 238.]
  [187. 215. 237.]
  ...
  [203. 219. 234.]
  [203. 219. 234.]
  [203. 219. 234.]]

 [[187. 215. 237.]
  [188. 216. 238.]
  [188. 216. 238.]
  ...
  [203. 219. 234.]
  [204. 220. 235.]
  [204. 220. 235.]]

 [[184. 212. 234.]
  [186. 214. 236.]
  [185. 213. 235.]
  ...
  [205. 221. 236.]
  [205. 221. 236.]
  [205. 221. 236.]]

 ...

 [[131. 154. 172.]
  [132. 155. 173.]
  [134. 156. 174.]
  ...
  [123. 136. 145.]
  [126. 139. 148.]
  [129. 142. 151.]]

 [[129. 152. 170.]
  [130. 153. 172.]
  [134. 156. 173.]
  ...
  [123. 136. 145.]
  [124. 137. 146.]
  [127. 140. 149.]]

 [[130. 153. 171.]
  [131. 154. 172.]
  [132. 154. 172.]
  ...
  [122. 135. 144.]
  [125. 138. 147.]
  [128. 141. 150.]]]

Also, Read – Python Projects with Source Code.

Summary

So this is how we can easily convert images to an array by using the Python programming language. Converting an image to an array is an important task to train a machine learning model based on the features of an image. I hope you liked this article on how to convert images into arrays using Python. Feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal
Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder
Articles: 1109

Leave a Reply