You are on page 1of 5

Part 4 Solution

Resize (Slide 20 and 21)

(PIL)
# Import Library
from PIL import Image

# Read Image and Convert to Grayscale


im = Image.open("lenna.gif")

# Resize Image((width, height))


im1 = im.resize((1024, 1024))
im2 = im.resize((256, 256))

# Display Image
im1.show()
im2.show() 1024

256
(OpenCV)
# Import Library
import cv2
256 1024
# Read Image
im = cv2.imread('Lenna.png')

# Resize Image(image, (width, height))


im1 = cv2.resize(im, (1024, 1024))
im2 = cv2.resize(im, (256, 256))

# Display Image(Windows Name, image)


cv2.imshow('Image 1', im1)
cv2.imshow('Image 2', im2)

cv2.waitKey()
Resize (Zoom) (Slide 22)

(PIL)
# Import Library
from PIL import Image

# Read Image
im = Image.open("lenna.gif")

# Obtain Width and Height


width, height = im.size 1024

# Resize Image((width, height))


im1 = im.resize((width*2, height*2))

# 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

# Resize Image(image, (height, width))


im1 = cv2.resize(im, (height*2, width*2))
596

# Display Image(Windows Name, image)


cv2.imshow('Image 1', im1)

cv2.waitKey()

print(im1.shape)
Part 5 Solution

Separate and Visualize Color Channel (Slide 8)

(PIL) – Solution 1 using putpixel


# Import Library
from PIL import Image

# Read Image
im = Image.open('leaf.jpg')

# Split 3 Channels
r, g, b = im.split()

# Get the width and height of image


width, height = im.size

# 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))

# Merge Image in RGB Mode


green_image = Image.merge('RGB', (r, g, b))

# Display Image
green_image.show()# Display Image

(PIL) – Solution 2 using lambda


# Import Library
from PIL import Image

# Read Image
im = Image.open('leaf.jpg')

# Split into 3 channels


r, g, b = im.split()

# Recombine back to RGB image


green_image = Image.merge('RGB', (r.point(lambda i: 0), g, b.point(lambda i: 0)))

# Display Image
green_image.show()
Extra Part Solution

Change the Top 50 rows of the images to White

(PIL) – Color Image


# Import Library
from PIL import Image

# 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()

(PIL) – Grayscale Image


# Import Library
from PIL import Image

# Read Image & Obtain Width and Height


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))

# Display
im1.show()

(OpenCV) – Color Image


# Import Library
import cv2

# 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

(PIL) – Grayscale Image


# Import Library
from PIL import Image

# Read Image, Convert to Grayscale & Obtain Height, Width and Channel
im1 = Image.open('Lenna.png').convert('L')
width, height = im1.size

# Set Border pixel as 50


border_px = 50

# 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()

(OpenCV) – Color Image


# Import Library
import cv2

# Read Image, Convert to Grayscale & Obtain Height, Width and Channel
im1 = cv2.imread('Lenna.png')
height, width, channel = im1.shape

# Set Border pixel as 50


border_px = 50

# 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)

You might also like