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 '[Link]
↪ANd9GcQwCYdvAtKsDTRLlMg9k-x97nZUpuDSHU_3Ng&s'
--2024-10-17 [Link]-- [Link]
[Link]/images?q=tbn:ANd9GcQwCYdvAtKsDTRLlMg9k-x97nZUpuDSHU_3Ng&s
Resolving [Link] ([Link])…
[Link], [Link], [Link], …
Connecting to [Link] (encrypted-
[Link])|[Link]|: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 [Link] (99.2 MB/s) -
‘images?q=tbn:ANd9GcQwCYdvAtKsDTRLlMg9k-x97nZUpuDSHU_3Ng&s’ saved [8900/8900]
1 Image Processing Libraries
1.1 1. [Link]
1.2 2. Pillow
1.3 3. OpenCV (cv2 : computer vision)
1.4 [Link]
[28]: # import the image module from [Link]
import [Link] as mpimg
import [Link] as plt
[30]: # load the image through [Link] module
img = [Link]('/content/space')
1
type(img)
[30]: [Link]
[45]: print([Link])
# 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 = [Link](img)
[Link]()
[Link]
[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 = [Link]('/content/space')
2
img1
[34]:
[35]: # resize the image
img1_resize = [Link]((300,300))
img1_resize
[35]:
3
[36]: # to save the resized file
img1_resize.save('space_image_resized.jpg')
[37]: img_resz = [Link]('/content/space_image_resized.jpg')
[Link](img_resz)
[Link]()
[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 = [Link]('/content/space')
type(img2)
print([Link])
4
(168, 300, 3)
[41]: # convert the RGB --> GrayScale
gray_scale_img = [Link](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
# [Link]() display the image but not work in collab
from [Link] import cv2_imshow
[43]: cv2_imshow(gray_scale_img)
[44]: # to save the gray image
[Link]('space_grayscale_img.jpg', gray_scale_img)
[44]: True
[ ]:
[ ]:
[ ]:
[ ]:
5
[ ]:
[ ]:
[ ]: