Replace raw file string with pathlib Path

This commit is contained in:
Tianyi Zheng 2023-04-01 15:16:25 -04:00
parent 4a3073e2e3
commit f1678839ad

View File

@ -1,3 +1,5 @@
from pathlib import Path
import numpy as np import numpy as np
from PIL import Image from PIL import Image
@ -63,11 +65,15 @@ def erosion(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:
if __name__ == "__main__": if __name__ == "__main__":
# read original image # read original image
lena = np.array(Image.open(r"..\image_data\lena.jpg")) lena_path = Path(__file__).resolve().parent / "image_data" / "lena.jpg"
lena = np.array(Image.open(lena_path))
# kernel to be applied # kernel to be applied
structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) structuring_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
# Apply erosion operation to a binary image # Apply erosion operation to a binary image
output = erosion(gray_to_binary(rgb_to_gray(lena)), structuring_element) output = erosion(gray_to_binary(rgb_to_gray(lena)), structuring_element)
# Save the output image # Save the output image
pil_img = Image.fromarray(output).convert("RGB") pil_img = Image.fromarray(output).convert("RGB")
pil_img.save("result_erosion.png") pil_img.save("result_erosion.png")