You are on page 1of 16

PAMANTASAN NG LUNGSOD NG MAYNILA

(University of the City of Manila)


Intramuros, Manila

ACTIVITY 2:
PROCESS A SINGLE IMAGE WITH
PILLOW
ELECTIVE 3 LABORATORY
CPE 0332.1-3

By:

GROUP 4:
ANG, JV B.
REYES, Martin Antonio M.

Submitted to
MA’AM EVELYN B. CAROLINO

July 20, 2022


Instructions: The image shall have basic changes such as rotation, resizing, creating thumbnail,
resampling methods, cropping image, RGB channels, converting to grayscale, converting to bitonal,
threshold for bitonal, and image page to bitonal.

Used Photo/Image:

Figure 1: Twice K-pop Group

Header Code Documentation:

Figure 2: Header Code using VS Code (Jupyter Type File)


SHOW ORIGINAL
OUTPUT/Documentation:

Figure 3: Original Image Result in VS Code (Jupyter Type File)


ROTATION
OUTPUT/Documentation:
Figure 4: Rotated Image Result in VS Code (Jupyter Type File)

RESIZING
OUTPUT/Documentation:

Figure 5: Resizing Image Result in VS Code (Jupyter Type File)


CREATING THUMBNAIL
OUTPUT/Documentation:

Figure 6: Creating Thumbnail Image Result in VS Code (Jupyter Type File)

RESAMPLING METHODS
OUTPUT/Documentation:

Figure 7: Resampling Methods Result in VS Code (Jupyter Type File)


CROPPING IMAGE
OUTPUT/Documentation:

Figure 8: Cropping Image Result in VS Code (Jupyter Type File)


RGB CHANNELS
OUTPUT/Documentation:
Figure 9: RGB Channel Results in VS Code (Jupyter Type File)
CONVERTING TO GRAYSCALE
OUTPUT/Documentation:

Figure 10: Converting to Grayscale Results in VS Code (Jupyter Type File)

CONVERTING TO BITONAL
OUTPUT/Documentation:
Figure 11: Converting to Bitonal Results in VS Code (Jupyter Type File)
THRESHOLD FOR BITONAL
OUTPUT/Documentation:

Figure 12: Threshold for Bitonal Results in VS Code (Jupyter Type File)
IMAGE PAGE TO BITONAL
OUTPUT/Documentation:

Figure 13: Image Page to Bitonal Results in VS Code (Jupyter Type File)
GROUP DOCUMENTATION
Full Syntax Code:
Creating Libraries

#imports
from PIL import Image
from pathlib import Path
import matplotlib.pyplot as plt

#opening img
img = Image.open("C:\Downloads\ELEC LAB\ACT 2\TWICESUPREMACY.jpg")

%matplotlib inline
plt.rcParams["figure.figsize"]=(20,12)
%config InlineBackline.figure_format='retina'

current_directory = Path.cwd()
img_path = current_directory.joinpath("C:\Downloads\ELEC LAB\ACT 2\TWICESUPREMACY.jpg")
print(f'current_directory:{current_directory}')
print(f'img_path:{img_path}')

Opening the image


image = Image.open(img_path)
plt.imshow(image)

Image Rotation
#rotation
rotated_image = image.rotate(23)
plt.imshow(rotated_image)

rotated_image = image.rotate(-23,resample=Image.BICUBIC,expand=True)
plt.imshow(rotated_image)

Resizing Image
#resizing
image_resized = image.resize((500,500))
plt.imshow(image_resized)

Creating Thumbnail
#thumbnail
thumbnail = image.copy()
thumbnail.thumbnail((500,500))
plt.imshow(thumbnail)

Resampling Methods
resample_dictionary = {'Nearest Neighbor':Image.NEAREST, 'Bilinear 2x2':Image.BILINEAR, 'Bicubic
4x4':Image.BICUBIC}
for name in resample_dictionary:
print(name)
resample_method = resample_dictionary[name]
%timeit rotated_image = image_resized.rotate(-23,resample=resample_method,expand=True)
Cropping Image
image_cropped = image.crop(box=(0,0,500,500))
plt.imshow(image_cropped)

image_cropped = image.crop(box=(200,210,500,500))
print(f'width:{image_cropped.size[0]}')
print(f'height:{image_cropped.size[1]}')
plt.imshow(image_cropped)

RGB Channels
#RGB channels
red_channel,green_channel,blue_channel = image_cropped.split()
figure,(red,green,blue) = plt.subplots(ncols=3,figsize=(18,8))

red.imshow(red_channel)
red.set_title("Red Channel")

green.imshow(green_channel)
green.set_title("Green Channel")

blue.imshow(blue_channel)
blue.set_title("Blue Channel")

plt.tight_layout()

color_bar = image_cropped.crop(box=[0,100,250,250])
plt.imshow(color_bar)

red_channel,green_channel,blue_channel = color_bar.split()
figure,(color,red,green,blue) = plt.subplots(nrows=4,figsize=(20,15))

color.imshow(color_bar)
color.set_xticks([]), color.set_yticks([])
color.set_title("RGB Image")

red.imshow(red_channel)
red.set_xticks([]), red.set_yticks([])
red.set_title("Red Channel")

green.imshow(green_channel)
green.set_xticks([]), green.set_yticks([])
green.set_title("Green Channel")

blue.imshow(blue_channel)
blue.set_xticks([]), blue.set_yticks([])
blue.set_title("Blue Channel")

plt.tight_layout()

Converting to Grayscale
#converting to grayscale
image_grayscale = color_bar.convert(mode='L')
plt.rcParams["figure.figsize"]=(20,12)
plt.imshow(image_grayscale)

Converting to Bitonal
#converting to bitonal
image_bitonal_floyd_steinberg = image_grayscale.convert(mode='1')
image_bitonal_no_dithering = image_grayscale.convert(mode='1',dither=Image.NONE)
plt.figure()
plt.imshow(image_grayscale)
plt.figure()
plt.imshow(image_bitonal_floyd_steinberg)
plt.figure()
plt.imshow(image_bitonal_no_dithering)

Setting Threshold for Bitonal Image


threshold = 85
image_bitonal_85 = image_grayscale.point(lambda pixel: pixel>threshold and 255)
plt.figure()
plt.imshow(image_bitonal_85)

threshold = 120
image_bitonal_120 = image_grayscale.point(lambda pixel: pixel>threshold and 255)
plt.figure()
plt.imshow(image_bitonal_120)

Image Page to Bitonal


image_page_cropped = image_cropped.crop(box=(0,100,300,460))
image_page_gray = image_page_cropped.convert(mode="L")
threshold = 185

image_page_bitonal = image_page_gray.point(lambda pixel: pixel>threshold and 255)


plt.imshow(image_page_bitonal)
Figure 14: Full Code in VS Code (Jupyter Type File)

Figure 15: Group Documentation for Task#2 (Discord Session)

You might also like