\[ \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}} \newcommand\pound{\unit{lbs}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{\kern-4mu}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{\kern-4mu}^\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} \]

Apr 28, 2026 | 296 words | 3 min read

15.2.1. Task 1#

Learning Objectives#

  • Compare different approaches to generating and saving images

  • Identify potential pitfalls when saving images using different methods

Task Instructions#

Open the Reflection Template py5_team_1_teamnumber.docx, and save it with your team’s number replacing teamnumber in the file name. Once completed, save the document as a PDF named py5_team_1_teamnumber.pdf.

Each team member should run their completed Section 15.1.2 code with both of the sample images from the pre task. Choose one team member’s working code file as the basis for this reflection and save a copy as py5_team_1_teamnumber.py

Reflection#

Answer the following questions in the Reflection Template:

  1. Are there any differences in how the following functions were implemented? What advantages and disadvantages do you see between the implementations?

    • normalize_image

    • linearize_image

    • mean_channel

  2. Did everyone structure their main function the same way? What differences stood out?

  3. Why is it important to normalize the image array before linearizing it?

  4. What would happen if the linearization step were applied before normalization?

  5. How do the shapes of RGB and grayscale images differ?

  6. What method(s) were used to determine whether an image was grayscale or RGB? Were all the used approaches equivalent?

Add the following function to your py5_team_1_teamnumber.py and call the function at the end of your main function (after displaying each channel mean).

The function takes in the NumPy array of linearized pixel values as its only argument.

import matplotlib.pyplot as plt

def compare_saved_images(image_array):
    # Convert the linearized image array back into integers in [0,255]
    image_array = (image_array * 255).astype(np.uint8)

    # Select the gray color mapping if image_array is grayscale
    cmap_select = None
    if len(image_array.shape) == 2 or (len(image_array.shape)==3 and image_array.shape[2] == 1):
        cmap_select = "gray"

    # Display and save the image_array using Matplotlib
    plt.imshow(image_array, cmap=cmap_select)
    plt.axis("off")

    # Save the figure with a bordering box of 0 (i.e., no bounding box)
    plt.savefig("image_from_figure.png", bbox_inches="tight", pad_inches=0)
    plt.close()

    # Save the image_array using a PIL Image object
    im = Image.fromarray(image_array)
    im.save("image_from_PIL_object.png")

    return
  1. Do the two saved images visually look different?

  2. If you open both saved images and check their file size or dimensions, are they identical? If not, why might they differ?

  3. Which method do you think is more appropriate if you want to preserve exact pixel values? Explain your reasoning.

  4. What potential issue could arise if you forget to convert the image back to integers before saving?

Table 15.4 Deliverables#

Deliverables

Description

py5_team_1_teamnumber.py

The Python code file chosen by the team for submission.

py5_team_1_teamnumber.pdf

The completed team reflection document.