Professional Documents
Culture Documents
(PIL)
# Import Library
from PIL import Image
# Display Image
im1.show()
im2.show() 1024
256
(OpenCV)
# Import Library
import cv2
256 1024
# Read Image
im = cv2.imread('Lenna.png')
cv2.waitKey()
Resize (Zoom) (Slide 22)
(PIL)
# Import Library
from PIL import Image
# Read Image
im = Image.open("lenna.gif")
# Display Image
im1.show()
print(im1.size)
1024
(OpenCV)
# Import Library
import cv2
# Read Image
im = cv2.imread('Lenna.png')
460
# Obtain Height, Width and Channel
height, width, channel = im.shape
cv2.waitKey()
print(im1.shape)
Part 5 Solution
# Read Image
im = Image.open('leaf.jpg')
# Split 3 Channels
r, g, b = im.split()
# Manipulate
for col in range(width):
for row in range(height):
# Change Red and Blue Channel to 0
r.putpixel((col, row), (0))
b.putpixel((col, row), (0))
# Display Image
green_image.show()# Display Image
# Read Image
im = Image.open('leaf.jpg')
# Display Image
green_image.show()
Extra Part Solution
# Read Image
im1 = Image.open('Cameraman_01.gif')
width, height = im1.size
# Manipulate
for h in range(50):
for w in range(width):
im1.putpixel((w, h), (255, 255, 255))
im1.show()
# Manipulate
for h in range(50):
for w in range(width):
im1.putpixel((w, h), (255, 255, 255))
# Display
im1.show()
# Read Image, Convert to Grayscale & Obtain Height, Width and Channel
im1 = cv2.imread('Lenna.png')
height, width, channel = im1.shape
# Manipulate
for h in range(50):
for w in range(width):
im1[h][w] = (255, 255, 255)
# Display
cv2.imshow('Image', im1)
cv2.waitKey(0)
Change the 4 Borders (50 pixels each) of the Image to White
# Read Image, Convert to Grayscale & Obtain Height, Width and Channel
im1 = Image.open('Lenna.png').convert('L')
width, height = im1.size
# Manipulate
for h in range(height):
for w in range(width):
if h < border_px or h > height - border_px:
im1.putpixel((w, h), (255))
else:
if w < border_px or w > width - border_px:
im1.putpixel((w, h), (255))
# Show Image
im1.show()
# Read Image, Convert to Grayscale & Obtain Height, Width and Channel
im1 = cv2.imread('Lenna.png')
height, width, channel = im1.shape
# Manipulate
for h in range(height):
for w in range(width):
if h < border_px or h > height - border_px:
im1[h][w] = (255, 255, 255)
else:
if w < border_px or w > width - border_px:
im1[h][w] = (255, 255, 255)
# Display Image
cv2.imshow('Image', im1)
cv2.waitKey(0)