\[ \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 | 574 words | 6 min read

15.1.2. Task 0#

Learning Objectives#

  • Utilize file I/O functions in Python to handle image data using modern Python libraries.

  • Analyze image properties such as color composition using manual looping methods.

  • Develop an understanding of Python libraries for image processing.

  • Display analysis results.

Introduction#

Most digital images store color using non-linear values called \(R'G'B'\) (sRGB). In an 8-bit image, each channel (\(R\), \(G\), \(B\)) is stored as an integer from \(0\) to \(255\). These values are gamma-encoded, which makes them efficient for display and storage but unsuitable for precise calculations such as computing brightness or comparing colors. In order to process the image, the image must first be normalized so that all values in the image are floating-point numbers between \(0.0\) and \(1.0\). Then by linearizing the normalized values, the gamma encoding is removed and accurate image manipulation and analysis can be done.

Task Instructions#

Develop a Python program that computes the average intensity of each channel in an image, and writes the results to a file.

Before creating the program, create a flowchart of the algorithm you will use and save it as py5_pre_0_username.pdf. Then start your program from a copy of the ENGR133_Python_Template.py Python template. Your program should be named py5_pre_0_username.py. Your program should do the following:

  1. Load the user specified image.

  2. Convert the image to a NumPy array.

  3. Normalize the pixel values in the NumPy array to the range \(0.0\)\(1.0\).

  4. Linearize the pixels in the NumPy array.

  5. Display the mean of the color channels.

Table 15.1 Image Download#

Images

Download Link

RGB image

color_image.jpeg

Grayscale image

grayscale_image.jpeg

Note

Reference Section 13.1.1 for help with NumPy and Section 15.1.1 for help with PIL.

Normalize Image Function#

Create a function named normalize_image that takes in an np.array that represents the image and returns the array after being normalized.

Normalize the pixel values in the array by dividing each value by \(255.0\). This is necessary for the subsequent linearization calculations.

Linearize Image Function#

Create a function named linearize_image that takes in a normalized np.array and returns the array after being linearized.

This is done using the transfer function shown in (15.1), where \(C'\) is the channel value before the transformation and \(C\) is the new channel value. This transfer function needs to be applied to each channel (\(R\), \(G\), and \(B\)) of every pixel in the array. The function applies one formula for darker values and another for brighter ones. For more background, see the sRGB article on Wikipedia.

(15.1)#\[\begin{split}C = \begin{cases} C' / 12.92, & C' \le 0.04045 \\ \left(\frac{C' + 0.055}{1.055}\right)^{2.4}, & C' > 0.04045 \end{cases}\end{split}\]

Mean Channel Function#

Create a function named mean_channel that takes in the linearized np.array and a channel index. The function will return the mean of specified color channel of the linearized array.

If the linearized array is grayscale, the mean of should be reported as zero. If img_array is the name of your linearized np.array then:

  • The red channel can be accessed using img_array[:, :, 0]

  • The green channel can be accessed using img_array[:, :, 1]

  • The blue channel can be accessed using img_array[:, :, 2]

Main Function#

In your main function you will need to do the following:

  1. Prompt the user for the name of image file and load the image using PIL.image.open.

  2. Convert the image to a NumPy array.

  3. Normalize the image array using the normalize_image function.

  4. Linearize the normalized image array using the linearize_image function.

  5. Calculate the mean for each channel (\(R\), \(G\), and \(B\)).

  6. Display the channel analysis.

Sample Output#

Test cases for the image processing Use the values in Table 15.2 below to test your program.

Table 15.2 Test Cases#

Case

image filename

1

color_image.jpeg

2

grayscale_image.jpeg

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 py5_pre_0_username.py Enter the filename of the image: color_image.jpeg Image: color_image.jpeg Red Channel Mean: 0.13 Green Channel Mean: 0.22 Blue Channel Mean: 0.33

Case 2 Sample Output

$ python3 py5_pre_0_username.py Enter the filename of the image: grayscale_image.jpeg Image: grayscale_image.jpeg Red Channel Mean: 0.00 Green Channel Mean: 0.00 Blue Channel Mean: 0.00

Table 15.3 Deliverables#

Deliverables

Description

py5_pre_0_username.pdf

Flowchart(s) for this task.

py5_pre_0_username.py

Your completed Python code.