Advertisement
Korotkodul

B. Часть 1. Паддингтон

Feb 23rd, 2025 (edited)
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. import numpy as np
  2.  
  3. def pad_image(image: np.ndarray, pad_size: int) -> np.ndarray:
  4.     if pad_size < 1:
  5.         raise ValueError
  6.     res = np.array([])
  7.     m = image.shape[0]
  8.     n = image.shape[1]
  9.     if image.ndim == 2:
  10.         res = np.zeros(shape = (m + 2 * pad_size, n + 2 * pad_size))
  11.     elif image.ndim == 3:
  12.         k = image.shape[2]
  13.         res = np.zeros(shape=(m + 2 * pad_size, n + 2 * pad_size, k))
  14.     res[pad_size: pad_size + m, pad_size: pad_size + n] = image
  15.     return res
  16.  
  17.  
  18. """
  19. matrix = np.array([
  20.    np.array([1,1,1,1,1,1,1]),
  21.    np.array([1,1,1,1,1,1,1]),
  22.    np.array([1,1,1,1,1,1,1])
  23. ])
  24.  
  25. res = pad_image(matrix, 2)
  26. print("res")
  27. print(res)
  28. """
  29.  
  30. MATRIX = np.full((2, 3, 7), 5)
  31. print(MATRIX)
  32.  
  33. RES = pad_image(MATRIX, 2)
  34. print("RES")
  35. print(RES)
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement