[+] A1 p4

This commit is contained in:
Hykilpikonna
2021-09-18 13:40:33 -04:00
parent 7f873afa85
commit 41ea5bcb40
+16 -5
View File
@@ -65,6 +65,9 @@ def add_pepper(pixel_data: list, k: int) -> list:
Because of the randomness, we can't specify an exact doctest.
"""
# I don't know why I have to use divide_channels here. I can just use (0,0,0)
return [random.choice([pixel] * k + [divide_channels(pixel, 256)]) for pixel in pixel_data]
def add_salt(pixel_data: list, k: int) -> list:
"""Return a new list of pixels formed from the corresponding pixels in pixel_data that has some
@@ -84,6 +87,9 @@ def add_salt(pixel_data: list, k: int) -> list:
Because of the randomness, we can't specify an exact doctest.
"""
# I don't know why I have to use maximize_channels here. I can just use (255,255,255)
return [random.choice([pixel] * k + [maximize_channels(pixel, 255)]) for pixel in pixel_data]
def add_salt_and_pepper(pixel_data: list, k: int) -> list:
"""Return a new list of pixels formed from the corresponding pixels in pixel_data
@@ -94,6 +100,9 @@ def add_salt_and_pepper(pixel_data: list, k: int) -> list:
Because of the randomness, we can't specify an exact doctest.
"""
pixel_data = add_pepper(pixel_data, k)
pixel_data = add_salt(pixel_data, k)
return pixel_data
def run_salt_and_pepper_example(source: str, destination: str, k: int) -> None:
@@ -112,10 +121,12 @@ if __name__ == '__main__':
import doctest
doctest.testmod()
# run_salt_and_pepper_example('images/horses.png', 'images/noisy-horses.png', 16)
# When you are ready to check your work with python_ta, uncomment the following lines.
# (Delete the "#" and space before each line.)
# import python_ta
# python_ta.check_all(config={
# 'extra-imports': ['random', 'a1_image'],
# 'max-line-length': 100
# })
import python_ta
python_ta.check_all(config={
'extra-imports': ['random', 'a1_image'],
'max-line-length': 100
})