


"""simple docstring"""

import string

import numpy


def lowerCAmelCase_   (					UpperCamelCase__						:			int					, UpperCamelCase__						:			int			):






 """simple docstring"""







 return b if a == 0 else greatest_common_divisor(b % a					, UpperCamelCase__			)





class  lowerCamelCase__    :
      a			:  str           =		string.ascii_uppercase + string.digits
      # This cipher takes alphanumerics into account
      # i.e. a total of 36 characters

      # take x and return x % len(key_string)
      a			:  Union[str, Any]           =		numpy.vectorize(lambda _a     : x % 36     )

      a			:  Optional[int]           =		numpy.vectorize(_a     )
      def __init__(		self						:						List[Any]   ,							A_						:						numpy.ndarray				):






       '''simple docstring'''


       __lowercase						=						self.modulus(A_				)  # mod36 calc's on the encrypt key
       self.check_determinant()  # validate the determinant of the encryption key
       __lowercase						=						encrypt_key.shape[0]
      def SCREAMING_SNAKE_CASE_						(		self						:						Dict   ,							A_						:						str				):






       '''simple docstring'''


       return self.key_string.index(A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Tuple   ,							A_						:						int				):






       '''simple docstring'''


       return self.key_string[round(A_				)]
      def SCREAMING_SNAKE_CASE_						(		self						:						int				):






       '''simple docstring'''


       __lowercase						=						round(numpy.linalg.det(self.encrypt_key				)				)

       if det < 0:
        __lowercase						=						det % len(self.key_string				)

       __lowercase						=						len(self.key_string				)
       if greatest_common_divisor(A_   ,							len(self.key_string				)				) != 1:
        __lowercase						=						(
            F'''determinant modular {req_l} of encryption key({det}) '''
            F'''is not co prime w.r.t {req_l}.\nTry another key.'''
        )
        raise ValueError(A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						Any   ,							A_						:						str				):






       '''simple docstring'''


       __lowercase						=						[char for char in text.upper() if char in self.key_string]

       __lowercase						=						chars[-1]
       while len(A_				) % self.break_key != 0:
        chars.append(A_				)

       return "".join(A_				)
      def SCREAMING_SNAKE_CASE_						(		self						:						List[Any]   ,							A_						:						str				):






       '''simple docstring'''


       __lowercase						=						self.process_text(text.upper()				)
       __lowercase						=						""""""

       for i in range(0   ,							len(A_				) - self.break_key + 1   ,							self.break_key				):
        __lowercase						=						text[i : i + self.break_key]
        __lowercase						=						[self.replace_letters(A_				) for char in batch]
        __lowercase						=						numpy.array([vec]				).T
        __lowercase						=						self.modulus(self.encrypt_key.dot(A_				)				).T.tolist()[
            0
        ]
        __lowercase						=						"""""".join(
            self.replace_digits(A_				) for num in batch_encrypted				)
        encrypted += encrypted_batch

       return encrypted
      def SCREAMING_SNAKE_CASE_						(		self						:						List[str]				):






       '''simple docstring'''


       __lowercase						=						round(numpy.linalg.det(self.encrypt_key				)				)

       if det < 0:
        __lowercase						=						det % len(self.key_string				)
       __lowercase						=						None
       for i in range(len(self.key_string				)				):
        if (det * i) % len(self.key_string				) == 1:
         __lowercase						=						i
         break

       __lowercase						=						(
           det_inv
           * numpy.linalg.det(self.encrypt_key				)
           * numpy.linalg.inv(self.encrypt_key				)
       )

       return self.to_int(self.modulus(A_				)				)






      def SCREAMING_SNAKE_CASE_						(		self						:						int   ,							A_						:						str				):






       '''simple docstring'''


       __lowercase						=						self.make_decrypt_key()
       __lowercase						=						self.process_text(text.upper()				)
       __lowercase						=						""""""

       for i in range(0   ,							len(A_				) - self.break_key + 1   ,							self.break_key				):
        __lowercase						=						text[i : i + self.break_key]
        __lowercase						=						[self.replace_letters(A_				) for char in batch]
        __lowercase						=						numpy.array([vec]				).T
        __lowercase						=						self.modulus(decrypt_key.dot(A_				)				).T.tolist()[0]
        __lowercase						=						"""""".join(
            self.replace_digits(A_				) for num in batch_decrypted				)
        decrypted += decrypted_batch

       return decrypted






def lowerCAmelCase_   (					):






 """simple docstring"""







 __lowercase						=						int(input("""Enter the order of the encryption key: """			)			)
 __lowercase						=						[]

 print("""Enter each row of the encryption key with space separated integers"""			)
 for _ in range(UpperCamelCase__			):
  __lowercase						=						[int(UpperCamelCase__			) for x in input().split()]
  hill_matrix.append(UpperCamelCase__			)

 __lowercase						=						HillCipher(numpy.array(UpperCamelCase__			)			)

 print("""Would you like to encrypt or decrypt some text? (1 or 2)"""			)
 __lowercase						=						input("""\n1. Encrypt\n2. Decrypt\n"""			)
 if option == "1":
  __lowercase						=						input("""What text would you like to encrypt?: """			)
  print("""Your encrypted text is:"""			)
  print(hc.encrypt(UpperCamelCase__			)			)
 elif option == "2":
  __lowercase						=						input("""What text would you like to decrypt?: """			)
  print("""Your decrypted text is:"""			)
  print(hc.decrypt(UpperCamelCase__			)			)


if __name__ == "__main__":
       import doctest

       doctest.testmod()

       main()




"""simple docstring"""

from pathlib import Path

import cva
import numpy as np
from matplotlib import pyplot as plt


def lowerCAmelCase_   (					UpperCamelCase__						:			np.ndarray					, UpperCamelCase__						:			np.ndarray					, UpperCamelCase__						:			np.ndarray					, UpperCamelCase__						:			int					, UpperCamelCase__						:			int			):






 """simple docstring"""







 __lowercase						=						cva.getAffineTransform(UpperCamelCase__					, UpperCamelCase__			)
 return cva.warpAffine(UpperCamelCase__					, UpperCamelCase__					, (rows, cols)			)


if __name__ == "__main__":
       # read original image
       UpperCAmelCase__					=cva.imread(
           str(Path(__file__).resolve().parent.parent / "image_data" / "lena.jpg")
       )
       # turn image in gray scale value
       UpperCAmelCase__					=cva.cvtColor(image, cva.COLOR_BGR2GRAY)
       # get image shape
       UpperCAmelCase__				,   UpperCAmelCase__					=gray_img.shape

       # set different points to rotate image
       UpperCAmelCase__					=np.array([[50, 50], [200, 50], [50, 200]], np.floataa)
       UpperCAmelCase__					=np.array([[10, 100], [200, 50], [100, 250]], np.floataa)
       UpperCAmelCase__					=np.array([[50, 50], [150, 50], [120, 200]], np.floataa)
       UpperCAmelCase__					=np.array([[10, 100], [80, 50], [180, 250]], np.floataa)

       # add all rotated images in a list
       UpperCAmelCase__					=[
           gray_img,
           get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
           get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
           get_rotation(gray_img, ptsa, ptsa, img_rows, img_cols),
       ]

       # plot different image rotations
       UpperCAmelCase__					=plt.figure(1)
       UpperCAmelCase__					=["Original", "Rotation 1", "Rotation 2", "Rotation 3"]
       for i, image in enumerate(images):
              plt.subplot(2, 2, i + 1), plt.imshow(image, "gray")
              plt.title(titles[i])
              plt.axis("off")
              plt.subplots_adjust(left=0.0, bottom=0.05, right=1.0, top=0.95)
       plt.show()
