Spaces:
Running
Running
Upload 3 files
Browse files- app.py +45 -0
- passport_img.py +73 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
import numpy as np
|
6 |
+
from passport_img import merge_all
|
7 |
+
|
8 |
+
def grayscale_image(image):
|
9 |
+
# Convert the image to grayscale
|
10 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
11 |
+
return gray
|
12 |
+
st.title('📸Passport Photo Maker📸')
|
13 |
+
def main():
|
14 |
+
# st.title("Grayscale Image Converter")
|
15 |
+
|
16 |
+
# File uploader
|
17 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
18 |
+
num_img = st.slider('Number of Image', 0, 36, 12)
|
19 |
+
if uploaded_file is not None:
|
20 |
+
if st.button('Make'):
|
21 |
+
# Read the image
|
22 |
+
image = Image.open(uploaded_file)
|
23 |
+
img_array = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
24 |
+
|
25 |
+
# st.image(image, caption='Original Image', use_column_width=True)
|
26 |
+
|
27 |
+
# Convert to grayscale
|
28 |
+
gray_image = merge_all(num_img,img_array)
|
29 |
+
gray_image = cv2.cvtColor(np.array(gray_image), cv2.COLOR_BGR2RGB)
|
30 |
+
|
31 |
+
pil_image = Image.fromarray(gray_image)
|
32 |
+
st.image(pil_image, caption='Passport Image', use_column_width=True)
|
33 |
+
|
34 |
+
# Create a download link for the grayscale image
|
35 |
+
buffered = io.BytesIO()
|
36 |
+
pil_image.save(buffered, format="PNG")
|
37 |
+
download_btn = st.download_button(
|
38 |
+
label='Download Image',
|
39 |
+
data=buffered.getvalue(),
|
40 |
+
file_name='passport_image.png',
|
41 |
+
mime='image/png'
|
42 |
+
)
|
43 |
+
|
44 |
+
if __name__ == '__main__':
|
45 |
+
main()
|
passport_img.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
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]))
|
16 |
+
|
17 |
+
# Save the cropped passport-size image
|
18 |
+
# w_output = add_border(output, 4, 4, 4, 4, (0,0,0))
|
19 |
+
# w_output = add_border(w_output, 15, 15, 15, 15, (255,255,255))
|
20 |
+
# cv2.imwrite(output_image_path, resized_image)
|
21 |
+
return resized_image
|
22 |
+
|
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 |
+
# cv2.imwrite(output_path, merge_img_final)
|
72 |
+
# print(merge_img_final.shape)
|
73 |
+
return merge_img_final
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
opencv
|