\[ \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 | 445 words | 4 min read

10.3.1. Task 1#

Image Analysis#

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.

  • Write analysis results to a text file.

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. To process the image, we first normalize the values by dividing by \(255.0\), giving floating-point numbers between \(0.0\) and \(1.0\). Next, we linearize these normalized \(R'G'B'\) values to remove the gamma encoding. This is done using the transfer function shown in (10.2), where \(C\) represents one of the three color channels (\(R\), \(G\), or \(B\)). The function applies one formula for darker values and another for brighter ones. For more background, see the sRGB article on Wikipedia.

(10.2)#\[\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}\]

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 py4_ind_1_username.pdf. Then start your program from a copy of the ENGR133_Python_Template.py Python template. Your program should be named py4_ind_1_username.py. Your program should do the following:

  1. Load the user specified image using PIL.image.open.

  2. Convert the image to a NumPy array for easier manipulation. See the official docs for usage details.

  3. Normalize the image’s pixel values to the range \(0\)\(1\) by dividing each value by \(255.0\). This is necessary for the subsequent linearization calculations.

  4. Linearize the pixels in the image by applying the transformation in (10.2). When applying the transformation, the ndim attribute may be useful for determining if the image is grayscale or color. See the official docs for usage details.

  5. Analyze the color content in the image by computing the mean of the linearized color channels. If the image is grayscale, the mean of should be reported as zero.

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

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

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

  6. Write the color analysis results into a text file named color_analysis.txt.

Table 10.7 Image Download#

Images

Download Link

RGB image

color_image.jpeg

Grayscale image

grayscale_image.jpeg

Sample Output#

Test cases for the image processing and histogram generation. Use the values in Table 10.8 below to test your program.

Table 10.8 Test Cases#

Case

image directory

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 py4_ind_1_username.py Enter the filename of the image: color_image.jpeg Color analysis results written to color_analysis.txt

Listing 10.2 Case_1_color_analysis.txt#
1Image: color_image.jpeg
2Red Channel Mean: 0.13
3Green Channel Mean: 0.22
4Blue Channel Mean: 0.33

Case 2 Sample Output

$ python3 py4_ind_1_username.py Enter the filename of the image: grayscale_image.jpeg Color analysis results written to color_analysis.txt

Listing 10.3 Case_2_color_analysis.txt#
1Image: grayscale_image.jpeg
2Red Channel Mean: 0.00
3Green Channel Mean: 0.00
4Blue Channel Mean: 0.00
Table 10.9 Deliverables#

Deliverables

Description

py4_ind_1_username.pdf

Flowchart(s) for this task.

py4_ind_1_username.py

Your completed Python code.