ahmet / Python-for-Data-Science
Herkese Açık Ahmet Kerem Piscine Final
pimp_image.py 2.0 KB
import numpy as np
import matplotlib.pyplot as plt
from load_image import ft_load
def ft_invert(array) -> np.ndarray:
"""Inverts the color of the image received."""
inverted = array.copy()
inverted[:, :, :] = 255 - inverted
return inverted
def ft_red(array) -> np.ndarray:
"""Keeps only the red channel of the image received."""
red = array.copy()
red[:, :, 1] = red[:, :, 1] * 0
red[:, :, 2] = red[:, :, 2] * 0
return red
def ft_green(array) -> np.ndarray:
"""Keeps only the green channel of the image received."""
green = array.copy()
green[:, :, 0] = green[:, :, 0] - green[:, :, 0]
green[:, :, 2] = green[:, :, 2] - green[:, :, 2]
return green
def ft_blue(array) -> np.ndarray:
"""Keeps only the blue channel of the image received."""
blue = array.copy()
blue[:, :, 0] = 0
blue[:, :, 1] = 0
return blue
def ft_grey(array) -> np.ndarray:
"""Converts the image received to greyscale."""
grey = array.copy()
average = array.sum(axis=2) / 3
grey[:, :, 0] = average
grey[:, :, 1] = average
grey[:, :, 2] = average
return grey
def main() -> None:
"""Load landscape.jpg, apply every filter and display the results."""
try:
array = ft_load("../landscape.jpg")
if array is None:
return
print(array)
images = [
("Original", array),
("Invert", ft_invert(array)),
("Red", ft_red(array)),
("Green", ft_green(array)),
("Blue", ft_blue(array)),
("Grey", ft_grey(array)),
]
figure, axes = plt.subplots(2, 3, figsize=(12, 6))
for axis, (title, image) in zip(axes.flat, images):
axis.imshow(image)
axis.set_title(title)
axis.axis("off")
figure.tight_layout()
plt.show()
print(ft_invert.__doc__)
except Exception as error:
print(f"Error: {error}")
if __name__ == "__main__":
main()