0% found this document useful (0 votes)
5 views

preprocessing-an-image

Uploaded by

jolehe1820
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

preprocessing-an-image

Uploaded by

jolehe1820
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

preprocessing-an-image

October 17, 2024

[29]: # getting an image using the web get , we can use to get any immage by the link

!wget 'https://encrypted-tbn0.gstatic.com/images?q=tbn:
↪ANd9GcQwCYdvAtKsDTRLlMg9k-x97nZUpuDSHU_3Ng&s'

--2024-10-17 11:03:18-- https://encrypted-


tbn0.gstatic.com/images?q=tbn:ANd9GcQwCYdvAtKsDTRLlMg9k-x97nZUpuDSHU_3Ng&s
Resolving encrypted-tbn0.gstatic.com (encrypted-tbn0.gstatic.com)…
142.250.141.100, 142.250.141.113, 142.250.141.139, …
Connecting to encrypted-tbn0.gstatic.com (encrypted-
tbn0.gstatic.com)|142.250.141.100|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 8900 (8.7K) [image/jpeg]
Saving to: ‘images?q=tbn:ANd9GcQwCYdvAtKsDTRLlMg9k-x97nZUpuDSHU_3Ng&s’

images?q=tbn:ANd9Gc 100%[===================>] 8.69K --.-KB/s in 0s

2024-10-17 11:03:18 (99.2 MB/s) -


‘images?q=tbn:ANd9GcQwCYdvAtKsDTRLlMg9k-x97nZUpuDSHU_3Ng&s’ saved [8900/8900]

1 Image Processing Libraries


1.1 1. matplotlib.image
1.2 2. Pillow
1.3 3. OpenCV (cv2 : computer vision)
1.4 Matplotlib.image
[28]: # import the image module from matplotlib.image

import matplotlib.image as mpimg


import matplotlib.pyplot as plt

[30]: # load the image through matplotlib.image module

img = mpimg.imread('/content/space')

1
type(img)

[30]: numpy.ndarray

[45]: print(img.shape)

# first and second values gives us the dimesion of the image and them third␣
↪value gives us the info about the color channel

(168, 300, 3)

[32]: # convert the numpy array to an image or display the image from the numpy array

img_plot = plt.imshow(img)
plt.show()
img.shape

[32]: (168, 300, 3)

1.4.1 Resize the image using the Pillow


• before feeding the image we need to make them into same dimension for the model
[33]: from PIL import Image

[34]: # to open the image


img1 = Image.open('/content/space')

2
img1
[34]:

[35]: # resize the image


img1_resize = img1.resize((300,300))
img1_resize
[35]:

3
[36]: # to save the resized file
img1_resize.save('space_image_resized.jpg')

[37]: img_resz = mpimg.imread('/content/space_image_resized.jpg')


plt.imshow(img_resz)
plt.show()

[38]: print(img_resz.shape)

(300, 300, 3)

1.5 Convert the Image RGB —> GrayScale


• for this we will use the cv2 library (opencv)

[39]: # import the opencv library


import cv2

[40]: img2 = cv2.imread('/content/space')


type(img2)

print(img2.shape)

4
(168, 300, 3)

[41]: # convert the RGB --> GrayScale

gray_scale_img = cv2.cvtColor(img2, cv2.COLOR_RGB2GRAY)


type(gray_scale_img)

# shape of the grayscale image


print(gray_scale_img.shape)

(168, 300)

[42]: # display the grayscale image

# cv2.imshow() display the image but not work in collab

from google.colab.patches import cv2_imshow

[43]: cv2_imshow(gray_scale_img)

[44]: # to save the gray image


cv2.imwrite('space_grayscale_img.jpg', gray_scale_img)

[44]: True

[ ]:

[ ]:

[ ]:

[ ]:

5
[ ]:

[ ]:

[ ]:

You might also like