## Question

## Transcribed Text

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

"""It creates and returns a histogram of a grayscale image.

A histogram provides a summary of the image by counting the number of pixels for each tonal value.

The list of counters are ordered based on the pixel value (i.e. from 0 to 255).

Input image should not be altered in any way.

"""

def histogram(image):

pixels = []

for i in range(256):

pixels.append(0)

for i in range(len(image)):

for j in range(len(image[0])):

pixels[image[i][j]] += 1

return pixels

"""

It takes a grayscale image and flips its content either horizontally or vertically.

No image is returned, all changes occur in place.

"""

def flip(image, orientation):

if orientation == 'horizontal':

for i in range(len(image)):

itm = image[i]

j = 0

k = len(itm) - 1

while j < k:

tmp = itm[j]

itm[j] = itm[k]

itm[k] = tmp

j += 1

k -= 1

elif orientation == 'vertical':

for i in range(len(image[0])):

j = 0

k = len(image) - 1

while j < k:

tmp = image[j][i]

image[j][i] = image[k][i]

image[k][i] = tmp

j += 1

k -= 1

"""

It takes a square grayscale image and rotates its content by 90 degrees clockwise.

No image is returned, all changes occur in place.

"""

def rotate(image):

n = len(image)

for i in range(int(n / 2)):

for j in range(i, n - i - 1):

temp = image[i][j]

image[i][j] = image[n - 1 - j][i]

image[n - 1 - j][i] = image[n - 1 - i][n - 1 - j]

image[n - 1 - i][n - 1 - j] = image[j][n - 1 - i]

image[j][n - 1 - i] = temp

"""

It returns a cropped tile out of a color image without altering the input in any way.

"""

def crop(image, origin, height, width):

result = []

for i in range(origin[0], origin[0] + height):

res = []

for j in range(origin[1], origin[1] + width):

res.append(image[i][j])

result.append(res)

return result

"""

It takes a color image (3D list) and returns a new grayscale image (2D list)

where each pixel will take the average value of the three color components truncated to integer.

Input image should not be altered in any way.

"""

def color2gray(image):

result = []

for i in range(len(image)):

res = []

for j in range(len(image[0])):

avg = int(sum(image[i][j]) / len(image[i][j]))

res.append(avg)

result.append(res)

return result...

By purchasing this solution you'll be able to access the following files:

Solution.py and Solution.PNG.