Skip to content

Jugal-Chanda/Convert-image-to-an-array-for-making-dataset

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Convert image to an array for making dataset

In this project at first we see how to load an image and show this using pillow.

Pillow is a python image library. It is by deafult installed by andconda. It is even required for simple image loading and saving in other Python scientific libraries such as Matplotlib.

Load image using pillow

from PIL import Image
img = Image.open('opera_house.jpg')

Summerize image details

print(img.format)
print(img.mode)
print(img.size)

Now see how to convert images to numpy array

For this at first we need to import asarray function from numpy. The asarray() function is used to convert an given input to an array. Input data, in any form that can be converted to an array. This includes lists, lists of tuples, tuples, tuples of tuples, tuples of lists and ndarrays.


Now we pass img object to the asarray() function and it returns an array.

#import library
from numpy import asarray
#convert image to numpy array which is loaded before
data = asarray(img)
We can see the type of this array using python python built in type() function and it returns the type of this array
type(data)
Check the shape of this array
print(data.shape)
Now we see also how to back mean how to create Pillow object from this numpy array
img2 = Image.fromarray(data)
img2

We can also do this by Matplotlib with pillow installed.

We can load image with imread() function.

At first we import image and pyplot from matplotlib

#Load libraries of matplotlib
from matplotlib import image,pyplot
#load image
data = image.imread('opera_house.jpg')

Now chekc the type of this data using the same thing we have done before.

type(data)

Now check the data details

print(data.dtype)
print(data.shape)

Now we show the image

pyplot.imshow(data)
pyplot.show()

For details Check This Code

Releases

No releases published

Packages

No packages published