mainakhf commited on
Commit
e1148db
·
1 Parent(s): c2de7a0

Update passport_img.py

Browse files
Files changed (1) hide show
  1. passport_img.py +41 -20
passport_img.py CHANGED
@@ -4,12 +4,16 @@ import matplotlib.pyplot as plt
4
 
5
 
6
 
7
- def crop_passport_size(img_arr):
 
 
 
 
8
  # Load the input image
9
  # original_image = cv2.imread(input_image_path)
10
  original_image = img_arr
11
  # Define the dimensions for the passport-size photo (35mm x 45mm)
12
- passport_size = (310, 410)
13
 
14
  # Resize the image to the passport-size dimensions
15
  resized_image = cv2.resize(original_image, (passport_size[0], passport_size[1]))
@@ -23,54 +27,71 @@ def crop_passport_size(img_arr):
23
  def add_border(image, top, bottom, left, right, color):
24
  return cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
25
 
26
- def horizontal_merge(number,crop_img):
27
- white_image = np.ones((450, 350, 3), dtype=np.uint8) * 255
 
 
 
 
 
 
28
  list_img = []
29
  # for i in range(5):
30
  for i in range(number):
31
  list_img.append(crop_img)
32
- for i in range(6-number):
33
  list_img.append(white_image)
34
  merge_img = cv2.hconcat(list_img)
35
  return merge_img
36
 
37
- def merge_all(number,input_path):
38
 
39
  input_image_path = input_path # Replace with the path to your input image
40
  # output_image_path = output_path # Replace with the desired output path
41
- output=crop_passport_size(input_image_path)
42
 
43
  w_output = add_border(output, 4, 4, 4, 4, (0,0,0))
44
  w_output = add_border(w_output, 15, 15, 15, 15, (255,255,255))
45
  w_output = add_border(w_output, 1, 1, 1, 1, (0,0,0))
46
 
 
 
 
 
 
 
 
47
  img_2 = []
48
  num_ = number
49
- if num_%6 == 0:
50
- for i in range(int(num_/6)):
51
- img1 = horizontal_merge(6,w_output)
52
  img_2.append(img1)
53
  # print('fill')
54
- for i in range(6-int(num_/6)):
55
- img1_1 = horizontal_merge(0,w_output)
56
  img_2.append(img1_1)
57
  # print('blank')
58
- elif num_%6 != 0:
59
- for i in range(int(num_//6)):
60
- img1 = horizontal_merge(6,w_output)
61
  img_2.append(img1)
62
  # print('fill')
63
- img1 = horizontal_merge(num_%6,w_output)
64
  img_2.append(img1)
65
  # print('some')
66
- for i in range(6-int(num_//6)-1):
67
- img1 = horizontal_merge(0,w_output)
68
  img_2.append(img1)
69
  # print('blank')
 
70
  merge_img_final = cv2.vconcat(img_2)
71
- bottom_part = np.ones((270, 2100, 3), dtype=np.uint8) * 255
 
72
  merge_img_final = cv2.vconcat([merge_img_final,bottom_part])
73
- merge_img_final = add_border(merge_img_final, 40, 20, 30, 30, (255,255,255))
 
 
74
  # cv2.imwrite(output_path, merge_img_final)
75
  # print(merge_img_final.shape)
76
  return merge_img_final
 
4
 
5
 
6
 
7
+ def crop_passport_size(img_arr,size):
8
+ if size==3545:
9
+ passport_size = (310, 410)
10
+ elif size==34:
11
+ passport_size = (260, 346)
12
  # Load the input image
13
  # original_image = cv2.imread(input_image_path)
14
  original_image = img_arr
15
  # Define the dimensions for the passport-size photo (35mm x 45mm)
16
+ # passport_size = (310, 410)
17
 
18
  # Resize the image to the passport-size dimensions
19
  resized_image = cv2.resize(original_image, (passport_size[0], passport_size[1]))
 
27
  def add_border(image, top, bottom, left, right, color):
28
  return cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)
29
 
30
+ def horizontal_merge(number,crop_img,size):
31
+ if size==3545:
32
+ white_image = np.ones((450, 350, 3), dtype=np.uint8) * 255
33
+ img_per_row = 6
34
+ elif size==34:
35
+ white_image = np.ones((386, 300, 3), dtype=np.uint8) * 255
36
+ img_per_row = 7
37
+ # white_image = np.ones((450, 350, 3), dtype=np.uint8) * 255
38
  list_img = []
39
  # for i in range(5):
40
  for i in range(number):
41
  list_img.append(crop_img)
42
+ for i in range(img_per_row-number):
43
  list_img.append(white_image)
44
  merge_img = cv2.hconcat(list_img)
45
  return merge_img
46
 
47
+ def merge_all(number,input_path,size):
48
 
49
  input_image_path = input_path # Replace with the path to your input image
50
  # output_image_path = output_path # Replace with the desired output path
51
+ output=crop_passport_size(input_image_path,size)
52
 
53
  w_output = add_border(output, 4, 4, 4, 4, (0,0,0))
54
  w_output = add_border(w_output, 15, 15, 15, 15, (255,255,255))
55
  w_output = add_border(w_output, 1, 1, 1, 1, (0,0,0))
56
 
57
+ if size==3545:
58
+ img_per_row = 6
59
+ bottom_part = np.ones((270, 2100, 3), dtype=np.uint8) * 255
60
+ elif size==34:
61
+ img_per_row = 7
62
+ bottom_part = np.ones((268, 2100, 3), dtype=np.uint8) * 255
63
+
64
  img_2 = []
65
  num_ = number
66
+ if num_%img_per_row == 0:
67
+ for i in range(int(num_/img_per_row)):
68
+ img1 = horizontal_merge(img_per_row,w_output,size)
69
  img_2.append(img1)
70
  # print('fill')
71
+ for i in range(img_per_row-int(num_/img_per_row)):
72
+ img1_1 = horizontal_merge(0,w_output,size)
73
  img_2.append(img1_1)
74
  # print('blank')
75
+ elif num_%img_per_row != 0:
76
+ for i in range(int(num_//img_per_row)):
77
+ img1 = horizontal_merge(img_per_row,w_output,size)
78
  img_2.append(img1)
79
  # print('fill')
80
+ img1 = horizontal_merge(num_%img_per_row,w_output,size)
81
  img_2.append(img1)
82
  # print('some')
83
+ for i in range(img_per_row-int(num_//img_per_row)-1):
84
+ img1 = horizontal_merge(0,w_output,size)
85
  img_2.append(img1)
86
  # print('blank')
87
+
88
  merge_img_final = cv2.vconcat(img_2)
89
+ # print(merge_img_final.shape)
90
+ # bottom_part = np.ones((270, 2100, 3), dtype=np.uint8) * 255
91
  merge_img_final = cv2.vconcat([merge_img_final,bottom_part])
92
+ # print(merge_img_final.shape)
93
+ merge_img_final = add_border(merge_img_final, 52, 32, 30, 30, (255,255,255))
94
+ # print(merge_img_final.shape)
95
  # cv2.imwrite(output_path, merge_img_final)
96
  # print(merge_img_final.shape)
97
  return merge_img_final