Python problem! Images as 2-D lists Please do not import any fancy functions, th
ID: 3861234 • Letter: P
Question
Python problem! Images as 2-D lists
Please do not import any fancy functions, this is only an intro class and we haven't gotten that far yet
Loading and saving pixels
At the top of ps10pr3.py, we’ve included an import statement for the hmcpng module, which was developed at Harvey Mudd College. This module gives you access to the following two functions that you will need for this problem:
load_pixels(filename), which takes as input a string filename that specifies the name of a PNG image file, and that returns a 2-D list containing the pixels for that image
save_pixels(pixels, filename), which takes a 2-D list pixels containing the pixels for an image and saves the corresponding PNG image to disk using the specified filename (a string).
In addition, we’ve given you a function create_uniform_image(height, width, pixel) that creates and returns a 2-D list of pixels with height rows and width columns in which all of the pixels have the RGB values given by pixel. Because all of the pixels will have the same color values, the resulting grid of pixels will produce an image with a uniform color – i.e., a solid block of that color.
Your tasks
Write a function blank_image(height, width) that creates and returns a 2-D list of pixels with height rows and width columns in which all of the pixels are green (i.e., have RGB values [0,255,0]). This function will be very easy to write if you take advantage of the create_uniform_image function that we’ve provided!
To test your function, re-run the file and enter the following from the Shell:
Write a function flip_horiz(pixels) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list of pixels for an image in which the original image is “flipped” horizontally. In other words, the left of the original image should now be on the right, and the right should now be on the left. For example, if you do the following:
This function will be similar to the flip_vert function that you wrote in [Problem Set 7][ps7], but it will flip the image horizontally instead of vertically. In addition, there are several other key differences:
It should take a 2-D list of pixels rather than a filename.
It does not need to load or save the image, because you will be doing that separately using the load_pixels and save_pixels functions.
It will manipulate the 2-D list of pixels, rather than an image object.
It should return the new 2-D list of pixels that it creates.
Your function will need to create a new 2-D list of pixels with the same dimensions as pixels, and you can use your blank_image function for that purpose.
Write a function flip_vert(pixels) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list of pixels for an image in which the original image is “flipped” vertically. In other words, the top of the original image should now be on the bottom, and the bottom should now be on the top. For example, if you do the following:
This function will be similar to the flip_horiz function that you wrote in [Problem Set 7][ps7], but it will flip the image vertically instead of horizontally. In addition, there are several other key differences:
It should take a 2-D list of pixels rather than a filename.
It does not need to load or save the image, because you will be doing that separately using the load_pixels and save_pixels functions.
It will manipulate the 2-D list of pixels, rather than an image object.
It should return the new 2-D list of pixels that it creates.
Your function will need to create a new 2-D list of pixels with the same dimensions as pixels, and you can use your blank_image function for that purpose.
Write a function transpose(pixels) that takes the 2-D list pixels containing pixels for an image, and that creates and returns a new 2-D list that is the transpose of pixels.
You should start by copy-and-pasting your transpose function from [Problem 3][pr3] into ps10pr3.py. The only real change that you need to make to your earlier function is to change the helper function that gets called to create the new 2-D list. Make this version of transpose call your blank_image function to create the new 2-D list. We also recommend changing the name of the variable matrix to pixels, but doing so is not essential.
Here’s one way to test your new version of transpose:
Write two more functions, both of which should take a 2-D list pixels containing pixels for an image, and create and return a new 2-D list of pixels for an image that is a rotation of the original image:
rotate_clockwise(pixels) should rotate the original image clockwise by 90 degrees. For example:
Both of these functions can be implemented very simply by taking advantage of the other functions that you have already written! In each case, think about how a sequence of two or more transformations of the original image could be used to produce the desired transformation.
Explanation / Answer
from PIL import Image, ImageFont, ImageDraw, ImageOps
# The image that we'll place the text on:
base_image = 'some_image.png'
# Now we'll create the transparent image used for writing the text on:
text_image_width = 250
text_image_height = 100
''' Initially the text image will be larger than its ideal size because
we'll need to downscale it later in order to make the text look less
pixelated. '''
resize_by = 4
text_image = Image.new(
'L',
(text_image_width * resize_by, text_image_height * resize_by)
)
font_file = 'some_font.otf'
font_size = 36 * resize_by
font_color = 255
font = ImageFont.truetype(font_file, font_size)
text = 'This is a test'
# Drawing the text on the text image:
draw = ImageDraw.Draw(text_image)
draw.text((0, 0), text, font=font, fill=font_color)
# Rotating the text image:
rotation_degrees = -10.5
text_image = text_image.rotate(rotation_degrees, expand=1)
# Resizing the text image in order to make the text look better:
text_image = text_image.resize(
(text_image_width, text_image_height),
Image.ANTIALIAS
)
# Pasting the rotated transparent text image onto the base image:
image = Image.open(base_image)
colorization = ImageOps.colorize(
text_image,
(0, 0, 0),
(255, 255, 255)
)
text_position_on_base_image = (200, 300)
image.paste(colorization, text_position_on_base_image, text_image)
image.show() # Or to save: image.save('image.png')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.