



'''simple docstring'''






from pathlib import Path

import numpy as np
from PIL import Image


def  snake_case_     (					_lowerCAmelCase  :						np.ndarray  )      ->   np.ndarray:
							UpperCAmelCase							,		UpperCAmelCase							,		UpperCAmelCase :       Any 							=		rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
							return 0.2_9_8_9 * r + 0.5_8_7_0 * g + 0.1_1_4_0 * b


def  snake_case_     (					_lowerCAmelCase  :						np.ndarray  )      ->   np.ndarray:
							return (gray > 127) & (gray <= 255)


def  snake_case_     (					_lowerCAmelCase  :						np.ndarray   ,      _lowerCAmelCase  :						np.ndarray  )      ->   np.ndarray:
							UpperCAmelCase :       int 							=		np.zeros_like(_lowerCAmelCase  )
							UpperCAmelCase :       Dict 							=		np.zeros(
							    (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1)  )

							# Copy image to padded image
							UpperCAmelCase :       Dict 							=		image

							# Iterate over image & apply kernel
							for x in range(image.shape[1]  ):
														for y in range(image.shape[0]  ):
																					UpperCAmelCase :       List[str] 							=		(
																					    kernel * image_padded[y : y + kernel.shape[0], x : x + kernel.shape[1]]
																					).sum()
																					UpperCAmelCase :       Any 							=		int(summation > 0  )
							return output


if __name__ == "__main__":
						# read original image
						UpperCamelCase__:   int								=			Path(__file__).resolve().parent / "image_data" / "lena.jpg"
						UpperCamelCase__:   Optional[Any]								=			np.array(Image.open(lena_path))
						# kernel to be applied
						UpperCamelCase__:   Union[str, Any]								=			np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
						UpperCamelCase__:   Optional[Any]								=			dilation(gray_to_binary(rgb_to_gray(lena)), structuring_element)
						# Save the output image
						UpperCamelCase__:   Union[str, Any]								=			Image.fromarray(output).convert("RGB")
						pil_img.save("result_dilation.png")










'''simple docstring'''






from __future__ import annotations


def  snake_case_     (					_lowerCAmelCase  :						str   ,      _lowerCAmelCase  :						str  )      ->   bool:
							UpperCAmelCase :       str 							=		get_failure_array(_lowerCAmelCase  )

							# 2) Step through text searching for pattern
							UpperCAmelCase							,		UpperCAmelCase :       Optional[Any] 							=		0, 0  # index into text, pattern
							while i < len(_lowerCAmelCase  ):
														if pattern[j] == text[i]:
																					if j == (len(_lowerCAmelCase  ) - 1):
																												return True
																					j += 1

														# if this is a prefix in our pattern
														# just go back far enough to continue
														elif j > 0:
																					UpperCAmelCase :       Optional[Any] 							=		failure[j - 1]
																					continue
														i += 1
							return False


def  snake_case_     (					_lowerCAmelCase  :						str  )      ->   list[int]:
							UpperCAmelCase :       Optional[Any] 							=		[0]
							UpperCAmelCase :       str 							=		0
							UpperCAmelCase :       List[str] 							=		1
							while j < len(_lowerCAmelCase  ):
														if pattern[i] == pattern[j]:
																					i += 1
														elif i > 0:
																					UpperCAmelCase :       Union[str, Any] 							=		failure[i - 1]
																					continue
														j += 1
														failure.append(_lowerCAmelCase  )
							return failure


if __name__ == "__main__":
						# Test 1)
						UpperCamelCase__:   str								=			"abc1abc12"
						UpperCamelCase__:   str								=			"alskfjaldsabc1abc1abc12k23adsfabcabc"
						UpperCamelCase__:   Any								=			"alskfjaldsk23adsfabcabc"
						assert kmp(pattern, texta) and not kmp(pattern, texta)

						# Test 2)
						UpperCamelCase__:   Tuple								=			"ABABX"
						UpperCamelCase__:   Union[str, Any]								=			"ABABZABABYABABX"
						assert kmp(pattern, text)

						# Test 3)
						UpperCamelCase__:   Any								=			"AAAB"
						UpperCamelCase__:   str								=			"ABAAAAAB"
						assert kmp(pattern, text)

						# Test 4)
						UpperCamelCase__:   int								=			"abcdabcy"
						UpperCamelCase__:   Any								=			"abcxabcdabxabcdabcdabcy"
						assert kmp(pattern, text)

						# Test 5)
						UpperCamelCase__:   List[str]								=			"aabaabaaa"
						assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]





