\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{{\kern-4mu}\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{\kern-4mu}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Capacitance \newcommand\farad{\unit{F}} \newcommand\F{\farad} \newcommand\microfarad{\micro\farad} \newcommand\muF{\micro\farad} % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Dec 04, 2025 | 555 words | 6 min read

11.2.2. Task 2#

Learning Objectives#

  • Clean images to prepare them for feature extraction and classification by resizing them to a consistent size.

Task Instructions#

Save the flowcharts for this task in tp1_team_2_teamnumber.pdf You will also need to include these flowcharts in your final report.

To prepare the images for analysis, they must all be a standard size. So, write Python code that resizes and pads any given image to a standard 100x100 pixel format. Name this program tp1_team_2_teamnumber.py.

Utilize the code you wrote in Task 1 to import and scale the pixel values to [0-255] before processing. Also, review Section 10.1.1 for documentation on using the PIL library and its functions.

  1. Write a clean_image function that has one argument: A NumPy array representing an image (the output from your load_img function). The function must return the cleaned image as a NumPy array. Your main method should call load_img and clean_image with the outputs matching the examples below.

    Note

    The clean_image function that you write, must be able to handle RGB and grayscale image arrays as inputs. This is shown in the examples below.

  2. The clean_image function must first resize the image to fit within a 100x100 canvas while maintaining its original aspect ratio to prevent distortion. Use the Pillow library’s resize() method with bilinear interpolation for this (see the docs).

(11.2)#\[\text{Aspect Ratio} = \frac{\text{Original Width}}{\text{Original Height}}\]
  1. When writing your code, consider the following three scenarios:

  • If the original image was wider than it was tall (landscape orientation), the resized image will have a new width of 100 pixels and a new height of \(\frac{100}{\text{Aspect Ratio}}\).

  • Conversely, if the original image was taller than it was wide (portrait orientation), the resized image will have a new height of 100 pixels and a new width of \({100} \times {\text{Aspect Ratio}}\).

  • If the original image was square, the resized image will be exactly 100x100 pixels.

Note

When calculating the new dimensions, round down to the nearest integer to avoid errors during resizing. We cannot have a fraction of a pixel.

  1. After resizing, the image will likely not be exactly 100x100 unless it was a square image to begin with. So, pad the image to reach the target dimensions. The padded area should be black. For this, you can use the ImageOps.pad() function (see the docs).

  2. Display the cleaned image to the user and print the original, resized, and final image size.

    Note

    The padded area (the “letterbox” bars) should be filled black. For color images, use the RGB value (0, 0, 0). For grayscale images, use the value 0.

    Additionally, for the method argument in the ImageOps.pad() function, use bilinear interpolation.

Hint

You will need to convert the input NumPy array image to a Pillow Image object and back to perform some steps. Review the following link:

Organize your code to use functions that break up the task into manageable pieces. Use the files provided in the Table 11.6 to test your code.

Table 11.6 Image Files#

Image File Name

Description

ref_col_raw0.png

A raw color image to be resized

ref_col_raw1.png

A raw color image to be resized

ref_col_raw2.png

A raw color image to be resized

ref_gry_raw.png

A raw grascale image to be resized

Sample Output#

Use the values in Table 11.7 below to test your program.

Table 11.7 Test Cases#

Case

image_path

1

ref_col_raw0.png

2

ref_col_raw1.png

3

ref_col_raw2.png

4

ref_gry_raw.png

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

$ python3 tp1_team_2_teamnumber.py Enter the path of the image you want to clean: ref_col_raw0.png Image shape before cleaning: (112, 121, 3) Resized image to: (92, 100) Image shape after cleaning: (100, 100, 3)

Case_1_output_ref_col_raw0.png

Fig. 11.4 Case_1_output_ref_col_raw0.png#

Case 2 Sample Output

$ python3 tp1_team_2_teamnumber.py Enter the path of the image you want to clean: ref_col_raw1.png Image shape before cleaning: (60, 59, 3) Resized image to: (100, 98) Image shape after cleaning: (100, 100, 3)

Case_2_output_ref_col_raw1.png

Fig. 11.5 Case_2_output_ref_col_raw1.png#

Case 3 Sample Output

$ python3 tp1_team_2_teamnumber.py Enter the path of the image you want to clean: ref_col_raw2.png Image shape before cleaning: (53, 52, 3) Resized image to: (100, 98) Image shape after cleaning: (100, 100, 3)

Case_3_output_ref_col_raw2.png

Fig. 11.6 Case_3_output_ref_col_raw2.png#

Case 4 Sample Output

$ python3 tp1_team_2_teamnumber.py Enter the path of the image you want to clean: ref_gry_raw.png Image shape before cleaning: (48, 50) Resized image to: (96, 100) Image shape after cleaning: (100, 100)

Case_4_output_ref_gry_raw.png

Fig. 11.7 Case_4_output_ref_gry_raw.png#

Table 11.8 Deliverables#

Deliverables

Description

tp1_team_2_teamnumber.pdf

Flowchart(s) for this task.

tp1_team_2_teamnumber.py

Your completed Python code.