






"""simple docstring"""





import numpy as np
from PIL import Image







def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE       )       ->						np.array:
    """simple docstring"""

    UpperCamelCase__							,    UpperCamelCase__							,    UpperCamelCase__								=     rgb[:, :, 0], rgb[:, :, 1], rgb[:, :, 2]
    return 0.2989 * r + 0.5870 * g + 0.1140 * b







def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE       )       ->						np.array:
    """simple docstring"""

    return (gray > 1_27) & (gray <= 2_55)







def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE       )       ->						np.array:
    """simple docstring"""

    UpperCamelCase__								=     np.zeros_like(SCREAMING_SNAKE_CASE       )
    UpperCamelCase__								=     np.zeros(
        (image.shape[0] + kernel.shape[0] - 1, image.shape[1] + kernel.shape[1] - 1)       )

    # Copy image to padded image
    UpperCamelCase__								=     image

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


# kernel to be applied
A__       :			Optional[Any]=						np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])

if __name__ == "__main__":
  # read original image
  A__       :			Optional[Any]=						np.array(Image.open(r"""..\image_data\lena.jpg"""))
  # Apply erosion operation to a binary image
  A__       :			Tuple=						erosion(grayabinary(rgbagray(image)), structuring_element)
  # Save the output image
  A__       :			Dict=						Image.fromarray(output).convert("""RGB""")
  pil_img.save("""result_erosion.png""")












"""simple docstring"""





from __future__ import annotations

from PIL import Image

# Define the first generation of cells
# fmt: off
A__       :			str=						[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]







def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE       )       ->						list[int]:
    """simple docstring"""

    return [int(SCREAMING_SNAKE_CASE       ) for c in F'{ruleset:08}'[:8]]







def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE       )       ->						list[int]:
    """simple docstring"""

    UpperCamelCase__								=     len(cells[0]       )  # 31
    UpperCamelCase__								=     []
    for i in range(SCREAMING_SNAKE_CASE       ):
        # Get the neighbors of each cell
        # Handle neighbours outside bounds by using 0 as their value
        UpperCamelCase__								=     0 if i == 0 else cells[time][i - 1]
        UpperCamelCase__								=     0 if i == population - 1 else cells[time][i + 1]
        # Define a new cell and add it to the new generation
        UpperCamelCase__								=     7 - int(F'{left_neighbor}{cells[time][i]}{right_neighbor}'			,						2       )
        next_generation.append(rule[situation]       )
    return next_generation







def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE       )       ->						Image.Image:
    """simple docstring"""

    UpperCamelCase__								=     Image.new('RGB'			,						(len(cells[0]       ), len(SCREAMING_SNAKE_CASE       ))       )
    UpperCamelCase__								=     img.load()
    # Generates image
    for w in range(img.width       ):
        for h in range(img.height       ):
            UpperCamelCase__								=     2_55 - int(2_55 * cells[h][w]       )
            UpperCamelCase__								=     (color, color, color)
    return img


if __name__ == "__main__":
  A__       :			Optional[int]=						bin(int(input("""Rule:\n""").strip()))[2:]
  A__       :			str=						format_ruleset(int(rule_num))
  for time in range(16):
    CELLS.append(new_generation(CELLS, rule, time))
  A__       :			Tuple=						generate_image(CELLS)
  # Uncomment to save the image
  # img.save(f"rule_{rule_num}.png")
  img.show()




