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:
Are there any differences in how the following functions were implemented? What advantages and disadvantages do you see between the implementations?
normalize_imagelinearize_imagemean_channel
Did everyone structure their
mainfunction the same way? What differences stood out?Why is it important to normalize the image array before linearizing it?
What would happen if the linearization step were applied before normalization?
How do the shapes of RGB and grayscale images differ?
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
Do the two saved images visually look different?
If you open both saved images and check their file size or dimensions, are they identical? If not, why might they differ?
Which method do you think is more appropriate if you want to preserve exact pixel values? Explain your reasoning.
What potential issue could arise if you forget to convert the image back to integers before saving?
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. |