File size: 58,210 Bytes
e4a0b82
 
43261b5
e4a0b82
 
 
 
 
 
 
 
69c4f29
43261b5
 
e4a0b82
9d86da5
bf53c7a
9d86da5
 
 
 
 
 
 
6e91837
49f407d
 
 
6e91837
49f407d
 
6e91837
49f407d
 
 
 
 
 
 
6e91837
 
9d86da5
 
 
 
 
 
 
 
 
 
 
bf53c7a
4adb5e7
9d86da5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e91837
fc34be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec4eb10
a484fa6
 
 
 
 
 
 
 
 
ec4eb10
a484fa6
 
ec4eb10
 
a484fa6
 
 
 
 
ec4eb10
a484fa6
 
 
 
 
ec4eb10
a484fa6
 
 
 
 
 
 
ec4eb10
 
fc34be6
 
 
 
 
 
6e91837
fc34be6
 
 
 
6e91837
fc34be6
 
 
 
 
e4a0b82
 
 
 
 
 
 
 
 
 
 
4fc7a47
bf53c7a
4adb5e7
e4a0b82
 
4fc7a47
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc34be6
6e91837
fc34be6
e4a0b82
fc34be6
 
e4a0b82
fc34be6
 
e4a0b82
fc34be6
 
e4a0b82
fc34be6
 
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43261b5
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43261b5
 
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43261b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b76ef6a
43261b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a0b82
 
4fc7a47
6e91837
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec4eb10
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a484fa6
 
e4a0b82
 
 
 
 
a484fa6
 
 
 
 
 
 
 
 
 
 
 
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e91837
a8d935d
2cecfbe
 
a8d935d
43261b5
 
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec4eb10
 
 
 
 
 
 
 
 
 
 
40aff70
 
 
 
 
 
 
 
 
 
43261b5
 
 
 
 
ec4eb10
 
43261b5
e4a0b82
 
 
a484fa6
e4a0b82
4fc7a47
6e91837
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec4eb10
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc34be6
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec4eb10
a484fa6
 
 
 
 
 
 
 
e4a0b82
a484fa6
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d3d7075
e4a0b82
 
d3d7075
 
 
e4a0b82
 
fc34be6
e4a0b82
 
 
 
 
 
 
fc34be6
 
 
e4a0b82
 
 
6e91837
 
5ecc6a1
ec4eb10
 
 
a484fa6
6e91837
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cecfbe
a8d935d
2cecfbe
a8d935d
fc34be6
e4a0b82
 
 
5c8c1f4
a484fa6
e4a0b82
 
 
 
b76ef6a
e4a0b82
 
ec4eb10
 
361eb9a
ec4eb10
e4a0b82
ec4eb10
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc34be6
 
43261b5
 
 
 
fc34be6
85c809d
43261b5
6e91837
e4a0b82
 
 
 
 
 
 
 
 
 
b76ef6a
e4a0b82
fc34be6
e4a0b82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a484fa6
e4a0b82
a484fa6
e4a0b82
 
b76ef6a
fc34be6
b76ef6a
fc34be6
 
b76ef6a
fc34be6
 
b76ef6a
fc34be6
b76ef6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a0b82
b76ef6a
47cba85
b76ef6a
6e91837
ee04c08
47cba85
b76ef6a
6e91837
de2f179
b76ef6a
de2f179
b76ef6a
 
 
 
 
 
 
 
 
 
 
6e91837
fc34be6
 
 
 
 
 
 
43261b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a7a5159
43261b5
 
 
 
fc34be6
 
 
 
 
 
 
 
43261b5
fc34be6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43261b5
fc34be6
43261b5
fc34be6
 
 
 
 
 
 
 
 
ec4eb10
6e91837
 
43261b5
 
 
40aff70
ec4eb10
43261b5
 
 
 
ec4eb10
9d86da5
9ebc09d
9d86da5
 
 
 
 
 
 
43261b5
 
 
 
 
 
 
 
 
e4a0b82
 
 
 
 
 
 
b76ef6a
 
e4a0b82
a9f7559
e4a0b82
43261b5
 
 
 
 
 
 
 
40aff70
e4a0b82
 
6c3f323
e4a0b82
 
a484fa6
e4a0b82
 
 
 
 
 
 
 
 
fc34be6
 
 
 
 
e4a0b82
 
 
 
fc34be6
e4a0b82
fc34be6
 
e4a0b82
40aff70
e4a0b82
43261b5
 
 
 
 
 
daf3b62
b76ef6a
40aff70
 
e4a0b82
 
 
 
 
 
 
 
ad11ab4
43261b5
 
 
 
 
a9f7559
e4a0b82
43261b5
e4a0b82
 
 
fc34be6
b76ef6a
 
e4a0b82
 
d3d7075
e4a0b82
 
 
 
 
a484fa6
 
 
6e91837
ec4eb10
 
40aff70
43261b5
e4a0b82
 
 
 
40aff70
6e91837
e4a0b82
 
 
 
 
 
 
 
 
43261b5
e4a0b82
40aff70
fc34be6
 
43261b5
fc34be6
 
 
 
 
 
6e91837
 
fc34be6
43261b5
40aff70
43261b5
 
fc34be6
 
40aff70
 
 
 
 
 
 
 
 
 
9d86da5
40aff70
9d86da5
 
fc34be6
9d86da5
bfdcaff
ec4eb10
 
 
 
 
 
40aff70
fc34be6
43261b5
6e91837
b76ef6a
43261b5
b76ef6a
 
 
e4a0b82
a484fa6
e4a0b82
 
 
ec4eb10
43261b5
 
 
e4a0b82
43261b5
 
6e91837
43261b5
b76ef6a
 
 
 
a484fa6
6e91837
 
 
 
a484fa6
e4a0b82
 
 
43261b5
40aff70
e4a0b82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
import gradio as gr
import numpy as np
from PIL import Image, ImageDraw, ImageFont, ImageColor, ImageFilter, ImageSequence
import os
import qrcode
from datetime import datetime
import pytz
import paramiko
import io
import re
import random
import zipfile
import math
import shutil

folder_path = "cache"  # ๋ฆฌ๋ฑํ† ๋ฆฌ/cache ๊ฒฝ๋กœ
ips = os.getenv("hjip")
# ํด๋”๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ
if os.path.exists(folder_path):
    print("Exist!")  # ํด๋”๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•  ๊ฒฝ์šฐ ์ถœ๋ ฅ
else:
    os.makedirs(folder_path)  # ํด๋”๊ฐ€ ์—†์œผ๋ฉด ํด๋” ์ƒ์„ฑ
    print(f"ํด๋” '{folder_path}'๋ฅผ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.")


def zip_folder(folder_path, output_path):
    # ์••์ถ•ํ•  ํด๋” ๊ฒฝ๋กœ
    folder_path = os.path.abspath(folder_path)

    # ์ถœ๋ ฅ ํŒŒ์ผ ๊ฒฝ๋กœ
    output_path = os.path.abspath(output_path)

    with zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
        # ํด๋” ๋‚ด ํŒŒ์ผ๋“ค์„ ์ˆœํšŒํ•˜๋ฉด์„œ ์••์ถ•
        for root, _, files in os.walk(folder_path):
            for file in files:
                absolute_path = os.path.join(root, file)
                relative_path = os.path.relpath(absolute_path, folder_path)
                zipf.write(absolute_path, relative_path)


def send_cache(zip_name):
    # ์••์ถ•ํ•  ํด๋” ์ด๋ฆ„
    folder_to_zip = 'cache'

    # ์••์ถ• ํŒŒ์ผ ์ด๋ฆ„
    output_zip = zip_name + '.zip'

    # ํด๋” ์••์ถ•
    zip_folder(folder_to_zip, output_zip)

    # SFTP ์—ฐ๊ฒฐ ์„ค์ •
    hostname = str(ips)
    port = 46834
    username = "hj6ch"
    password = "mine06^^"

    transport = paramiko.Transport((hostname, port))
    transport.connect(username=username, password=password)
    sftp = paramiko.SFTPClient.from_transport(transport)

    # ํŒŒ์ผ ์ „์†ก
    local_path = output_zip
    remote_path = f'/home/hj6ch/outputs/{output_zip}'
    sftp.put(local_path, remote_path)

    # ์—ฐ๊ฒฐ ์ข…๋ฃŒ
    sftp.close()
    transport.close()

    # ์••์ถ• ํŒŒ์ผ ์‚ญ์ œ
    os.remove(output_zip)

    # ํด๋” ๋‚ด ํŒŒ์ผ ์‚ญ์ œ
    folder_path = folder_to_zip
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            os.remove(os.path.join(root, file))


def water_big(text):
    font_size = 60
    fonts = "Pretendard-SemiBold.otf"
    image_size = (600, 100)
    # ์ด๋ฏธ์ง€์™€ ๋“œ๋กœ์ž‰ ๊ฐ์ฒด ์ƒ์„ฑ
    image = Image.new('RGBA', image_size, (255, 255, 255, 0))  # ํˆฌ๋ช…ํ•œ ๋ฐฐ๊ฒฝ
    draw = ImageDraw.Draw(image)

    # ๊ธ€๊ผด ์„ค์ •
    font = ImageFont.truetype(fonts, font_size)
    text_color = (255, 255, 255)
    # ํ…์ŠคํŠธ ํฌ๊ธฐ ๊ณ„์‚ฐ
    text_bbox = draw.textbbox((0, 0), text, font=font)
    text_width = text_bbox[2] - text_bbox[0]
    text_height = text_bbox[3] - text_bbox[1]

    # ํ…์ŠคํŠธ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ทธ๋ฆฌ๊ธฐ
    text_x = 0  # ์™ผ์ชฝ ๋งž์ถค
    text_y = (image_size[1] - text_height) / 2
    draw.text((text_x, text_y), text, fill=text_color, font=font)

    # ์ด๋ฏธ์ง€ ํšŒ์ „
    rotated_image = image.rotate(313.4, expand=True)
    return rotated_image


def water_small(text):
    font_size = 10
    fonts = "Pretendard-SemiBold.otf"
    image_size = (100, 50)
    # ์ด๋ฏธ์ง€์™€ ๋“œ๋กœ์ž‰ ๊ฐ์ฒด ์ƒ์„ฑ
    image = Image.new('RGBA', image_size, (255, 255, 255, 0))  # ํˆฌ๋ช…ํ•œ ๋ฐฐ๊ฒฝ
    draw = ImageDraw.Draw(image)

    # ๊ธ€๊ผด ์„ค์ •
    font = ImageFont.truetype(fonts, font_size)
    text_color = (255, 255, 255)
    # ํ…์ŠคํŠธ ํฌ๊ธฐ ๊ณ„์‚ฐ
    text_bbox = draw.textbbox((0, 0), text, font=font)
    text_width = text_bbox[2] - text_bbox[0]
    text_height = text_bbox[3] - text_bbox[1]

    # ํ…์ŠคํŠธ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ทธ๋ฆฌ๊ธฐ
    text_x = 0  # ์™ผ์ชฝ ๋งž์ถค
    text_y = (image_size[1] - text_height) / 2
    draw.text((text_x, text_y), text, fill=text_color, font=font)
    rotated_image = image.rotate(10.2, expand=True)
    return rotated_image


def gradient_replace_white(img):
    # ์ด๋ฏธ์ง€ ์—ด๊ธฐ

    # ์ด๋ฏธ์ง€ RGBA ๋ชจ๋“œ๋กœ ๋ณ€ํ™˜
    img = img.convert("RGBA")

    # ์ด๋ฏธ์ง€์˜ ๋„ˆ๋น„์™€ ๋†’์ด ๊ตฌํ•˜๊ธฐ
    width, height = (1164, 1800)

    # ํ”ฝ์…€ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
    pixels = img.load()

    # ํฐ์ƒ‰์ธ ํ”ฝ์…€์„ ์•Œ๋ก๋‹ฌ๋กํ•œ ๊ทธ๋ผ๋ฐ์ด์…˜์œผ๋กœ ๋ณ€๊ฒฝ
    for y in range(height):
        for x in range(width):
            r, g, b, a = pixels[x, y]
            # ํฐ์ƒ‰์ธ ๋ถ€๋ถ„๋งŒ ์ฒ˜๋ฆฌ
            if (r, g, b) == (255, 255, 255):
                # ์•Œ๋ก๋‹ฌ๋กํ•œ ๊ทธ๋ผ๋ฐ์ด์…˜ ๊ณ„์‚ฐ
                new_r = x % 256  # x ์ขŒํ‘œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋นจ๊ฐ„์ƒ‰ ๊ฐ’ ๊ณ„์‚ฐ
                new_g = y % 256  # y ์ขŒํ‘œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ดˆ๋ก์ƒ‰ ๊ฐ’ ๊ณ„์‚ฐ
                new_b = (x + y) % 256  # x, y ์ขŒํ‘œ ํ•ฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํŒŒ๋ž€์ƒ‰ ๊ฐ’ ๊ณ„์‚ฐ

                # ํ”ฝ์…€ ๊ฐ’ ๋ณ€๊ฒฝ
                pixels[x, y] = (new_r, new_g, new_b, a)
    return img
    # ๋ณ€๊ฒฝ๋œ ์ด๋ฏธ์ง€ ์ €์žฅ


def get_text_end_coordinates(text, font_path, font_size, x, y, color):
    # ์ด๋ฏธ์ง€ ์—ด๊ธฐ
    image_size = (1600, 136)
    image = Image.new('RGBA', image_size, (255, 255, 255, 0))  # ํˆฌ๋ช…ํ•œ ๋ฐฐ๊ฒฝ
    # ํฐํŠธ ์„ค์ •
    font = ImageFont.truetype(font_path, font_size)

    # ์ด๋ฏธ์ง€์— ํ…์ŠคํŠธ ์ถ”๊ฐ€
    draw = ImageDraw.Draw(image)
    draw.text((x, y), text, font=font, fill=color)  # ์ด๋ฏธ์ง€์— ํ…์ŠคํŠธ ๊ทธ๋ฆฌ๊ธฐ

    # ํ…์ŠคํŠธ ํฌ๊ธฐ ๊ตฌํ•˜๊ธฐ
    text_width = draw.textlength(text, font=font)  # textsize ๋Œ€์‹  textlength ์‚ฌ์šฉ
    text_height = font_size  # textsize ๋Œ€์‹  font_size ์‚ฌ์šฉ

    # ํ…์ŠคํŠธ ๋๋‚˜๋Š” ์ขŒํ‘œ ๊ณ„์‚ฐ
    end_x = x + text_width
    end_y = y + text_height

    return image, end_x, end_y  # image ๊ฐ์ฒด ๋ฐ˜ํ™˜


def season_out(text_to_write, text_to_write2, color):
    font_path = 'Helvetica_Neue_LT_Std_65_Medium.otf'  # ์‚ฌ์šฉํ•  ํฐํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ
    font_size = 130  # ํฐํŠธ ํฌ๊ธฐ
    x, y = 0, 0  # ํ…์ŠคํŠธ ์‹œ์ž‘ ์ขŒํ‘œ

    img, x1, y1 = get_text_end_coordinates(text_to_write, font_path, font_size, x, y, color)
    font_path = 'Helvetica_Neue_LT_Std_75_Bold_Outline.otf'  # ์‚ฌ์šฉํ•  ํฐํŠธ ํŒŒ์ผ ๊ฒฝ๋กœ
    font_size = 135
    img2, x, y = get_text_end_coordinates(text_to_write2, font_path, font_size, x, y, color)
    img.paste(img2, (int(x1 + 2), 0), img2)
    return img


def water_mark(text):
    # ์ด๋ฏธ์ง€ ์ €์žฅ
    img = Image.new(mode="RGBA", size=(1164, 1800))
    rotated_image = water_big(text)
    for i in range(4):
        for a in range(4):
            img.paste(rotated_image, (i * 300, a * 500), rotated_image)

    rotated_image = water_small(text)
    for i in range(12):
        for a in range(40):
            img.paste(rotated_image, (i * 100 + 20, a * 50), rotated_image)

    img = gradient_replace_white(img)
    return img


def get_kr_time():
    # ํ•œ๊ตญ ์‹œ๊ฐ„๋Œ€ ์„ค์ •
    korea_time_zone = pytz.timezone('Asia/Seoul')

    # ํ˜„์žฌ ์‹œ๊ฐ„์„ ํ•œ๊ตญ ์‹œ๊ฐ„๋Œ€๋กœ ๋ณ€ํ™˜
    current_time_in_korea = datetime.now(korea_time_zone)

    # ์›ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ์‹œ๊ฐ„ ํฌ๋งทํŒ…
    return current_time_in_korea.strftime("%Y%m%d-%H%M%S")


def upload_sftp(image, name):
    host = str(ips)
    port = 46834
    username = "hj6ch"
    password = "mine06^^"
    remote_path = f"/home/hj6ch/outputs/{name}"

    # ์ด๋ฏธ์ง€๋ฅผ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜
    img_byte_arr = io.BytesIO()
    image.save(img_byte_arr, format='PNG')
    img_byte_arr.seek(0)

    try:
        # SFTP ์„ธ์…˜ ์‹œ์ž‘
        transport = paramiko.Transport((host, port))
        transport.connect(username=username, password=password)

        sftp = paramiko.SFTPClient.from_transport(transport)

        # ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์„ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ ์—…๋กœ๋“œ
        sftp.putfo(img_byte_arr, remote_path)
        print(f"Image uploaded to {remote_path}")

        # ์—ฐ๊ฒฐ ์ข…๋ฃŒ
        sftp.close()
        transport.close()
    except Exception as e:
        print(f"An error occurred: {e}")


def create_qr_with_icon(url, icon_path, resolution=(335, 335), border_reduction=2):
    # Generate QR code with a smaller border
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=4 - border_reduction,  # Reduce the border by the border_reduction amount
    )
    qr.add_data(url)
    qr.make(fit=True)

    # Create an image from the QR Code instance
    qr_img = qr.make_image(fill_color="black", back_color="white").convert('RGB')

    # Resize QR code to the specified resolution
    # Instead of using Image.ANTIALIAS, you should now use Image.Resampling.LANCZOS
    qr_img = qr_img.resize(resolution, Image.Resampling.LANCZOS)
    if icon_path == "tripleS Logo":
        # Open the icon file
        icon_img = Image.open('qr_icon.png')

        # Calculate the maximum size of the icon
        icon_size = min(resolution) // 4

        # Resize the icon, if it's too large
        icon_img.thumbnail((icon_size, icon_size), Image.Resampling.LANCZOS)

        # Calculate position for icon (center)
        icon_pos = ((resolution[0] - icon_img.size[0]) // 2, (resolution[1] - icon_img.size[1]) // 2)

        # Paste the icon image onto the QR code
        qr_img.paste(icon_img, icon_pos, icon_img)

    # Save the QR code with icon

    return qr_img


def gnames(text, r, g, b, font):
    # ์ด๋ฏธ์ง€ ํฌ๊ธฐ์™€ ๊ธ€๊ผด ํฌ๊ธฐ ์„ค์ •
    image_size = (400, 100)  # ๋„ˆ๋น„ x ๋†’์ด
    font_size = font

    # ์ด๋ฏธ์ง€์™€ ๋“œ๋กœ์ž‰ ๊ฐ์ฒด ์ƒ์„ฑ
    image = Image.new('RGBA', image_size, (255, 255, 255, 0))  # ํˆฌ๋ช…ํ•œ ๋ฐฐ๊ฒฝ
    draw = ImageDraw.Draw(image)

    # ๊ธ€๊ผด ์„ค์ •
    font = ImageFont.truetype("Helvetica_Neue_LT_Std_75_Bold.otf", font_size)
    text_color = (r, g, b)

    # ํ…์ŠคํŠธ ๊ทธ๋ฆฌ๊ธฐ
    text_bbox = draw.textbbox((0, 0), text, font=font)
    text_width = text_bbox[2] - text_bbox[0]
    text_height = text_bbox[3] - text_bbox[1]
    text_x = image_size[0] - text_width  # ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ์„ ์œ„ํ•ด ์ˆ˜์ •
    text_y = (image_size[1] - text_height) / 2
    draw.text((text_x, text_y), text, fill=text_color, font=font)

    # ์ด๋ฏธ์ง€ ํšŒ์ „
    rotated_image = image.rotate(270, expand=True)
    return rotated_image


#from PIL import Image, ImageDraw, ImageFont


def shop(text, r, g, b, fs, fonts):
    # ์ด๋ฏธ์ง€ ํฌ๊ธฐ์™€ ๊ธ€๊ผด ํฌ๊ธฐ ์„ค์ •
    image_size = (400, 100)  # ๋„ˆ๋น„ x ๋†’์ด
    font_size = fs
    spacing = 43  # ๊ธ€์ž ๊ฐ„ ๊ฐ„๊ฒฉ

    # ์ด๋ฏธ์ง€์™€ ๋“œ๋กœ์ž‰ ๊ฐ์ฒด ์ƒ์„ฑ
    image = Image.new('RGBA', image_size, (255, 255, 255, 0))  # ํˆฌ๋ช…ํ•œ ๋ฐฐ๊ฒฝ
    draw = ImageDraw.Draw(image)

    # ๊ธ€๊ผด ์„ค์ •
    font = ImageFont.truetype(fonts, font_size)
    text_color = (r, g, b)

    # ํ…์ŠคํŠธ ์œ„์น˜ ์ดˆ๊ธฐํ™”
    current_x = 0
    text_y = (image_size[1] - font_size) // 2  # ์„ธ๋กœ ์ค‘์•™ ์ •๋ ฌ

    # ๊ฐ ๊ธ€์ž๋ฅผ ๊ฐœ๋ณ„์ ์œผ๋กœ ๊ทธ๋ฆฌ๊ธฐ
    for char in text:
        draw.text((current_x, text_y), char, fill=text_color, font=font)
        current_x += spacing

    # ์ด๋ฏธ์ง€ ํšŒ์ „
    rotated_image = image.rotate(270, expand=True)

    return rotated_image


def names(text, r, g, b, fs, fonts, rotate, tes):
    # ์ด๋ฏธ์ง€ ํฌ๊ธฐ์™€ ๊ธ€๊ผด ํฌ๊ธฐ ์„ค์ •
    if tes == False:
        image_size = (400, 100)
    else:
        image_size = (800, 136)  # ๋„ˆ๋น„ x ๋†’์ด
    font_size = fs

    # ์ด๋ฏธ์ง€์™€ ๋“œ๋กœ์ž‰ ๊ฐ์ฒด ์ƒ์„ฑ
    image = Image.new('RGBA', image_size, (255, 255, 255, 0))  # ํˆฌ๋ช…ํ•œ ๋ฐฐ๊ฒฝ
    draw = ImageDraw.Draw(image)

    # ๊ธ€๊ผด ์„ค์ •
    font = ImageFont.truetype(fonts, font_size)
    text_color = (r, g, b)
    # ํ…์ŠคํŠธ ํฌ๊ธฐ ๊ณ„์‚ฐ
    text_bbox = draw.textbbox((0, 0), text, font=font)
    text_width = text_bbox[2] - text_bbox[0]
    text_height = text_bbox[3] - text_bbox[1]

    # ํ…์ŠคํŠธ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ •๋ ฌํ•˜์—ฌ ๊ทธ๋ฆฌ๊ธฐ
    text_x = 0  # ์™ผ์ชฝ ๋งž์ถค
    text_y = (image_size[1] - text_height) / 2
    draw.text((text_x, text_y), text, fill=text_color, font=font)

    # ์ด๋ฏธ์ง€ ํšŒ์ „
    if rotate:
        rotated_image = image.rotate(270, expand=True)
    else:
        rotated_image = image.rotate(0, expand=True)
    return rotated_image


def create_text_image(text, r, g, b):
    """
    Create an image with the specified text.
    The text uses two different fonts: normal for letters and outline for numbers.
    The text color is defined by the RGB values (r, g, b).

    :param text: The text to be drawn.
    :param r: Red component of the color, range 0-255.
    :param g: Green component of the color, range 0-255.
    :param b: Blue component of the color, range 0-255.
    :return: An image object with the drawn text.
    """

    # Estimate the width of the image based on the text length
    estimated_width = sum(
        [ImageFont.ImageFont.getsize(font=normal_font if char.isalpha() else outline_font, text=char)[0] for char in
         text]) + 10 * len(text)
    # Create an image with a transparent background
    img = Image.new('RGBA', (estimated_width, 100), color=(255, 255, 255, 0))
    d = ImageDraw.Draw(img)

    # Current position for drawing text
    current_position = 10

    # Draw the text with the specified color
    for char in text:
        # Select the appropriate font
        used_font = outline_font if char.isdigit() else normal_font
        # Draw the character
        d.text((current_position, 10), char, font=used_font, fill=(r, g, b, 255))
        # Calculate width of the character and update position
        width, _ = ImageFont.ImageFont.getsize(font=used_font, text=char)
        current_position += width + 5  # Added a small space between characters

    return img


def hex_to_rgb(hex_color):
    # Remove the '#' character if it exists
    hex_color = hex_color.lstrip('#')

    # Convert the hex string to integers
    r, g, b = tuple(int(hex_color[i:i + 2], 16) for i in (0, 2, 4))
    return r, g, b


def load_image_as_pil(file_path):
    """Load an image from the specified file path and return it as a PIL Image object."""
    try:
        with Image.open(file_path) as img:
            return img.copy()
    except Exception as e:
        return str(e)


def resize_and_crop_image(image, target_width, target_height):
    # ์ด๋ฏธ์ง€๋ฅผ PIL Image ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜
    #img = Image.fromarray(image.astype('uint8'), 'RGB')
    img = image
    # ์›๋ณธ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์™€ ๋น„์œจ ๊ณ„์‚ฐ
    original_width, original_height = img.size
    ratio = min(original_width / target_width, original_height / target_height)

    # ์ƒˆ๋กœ์šด ํฌ๊ธฐ ๊ณ„์‚ฐ
    new_width = int(target_width * ratio)
    new_height = int(target_height * ratio)

    # ์ด๋ฏธ์ง€๋ฅผ ์ค‘์•™์—์„œ ์ž๋ฆ„
    img = img.crop(((original_width - new_width) // 2,
                    (original_height - new_height) // 2,
                    (original_width + new_width) // 2,
                    (original_height + new_height) // 2))

    # ์ตœ์ข…์ ์œผ๋กœ ๋ชฉํ‘œ ํ•ด์ƒ๋„๋กœ ๋ฆฌ์‚ฌ์ด์ฆˆ
    img = img.resize((target_width, target_height), Image.Resampling.LANCZOS)

    return img


def change_color(objekt_color, nn):
    file_path = nn  # Adjust the path according to your setup
    icon = Image.open(file_path).convert("RGBA")
    image_np = np.array(icon)
    _, _, _, alpha = image_np.T
    mask = alpha > 0
    image_np[..., :-1][mask.T] = ImageColor.getcolor(objekt_color, "RGB")
    colored_icon = Image.fromarray(image_np)
    return colored_icon


def no_change_color():
    file_path = 'spe.png'  # Adjust the path according to your setup
    icon = Image.open(file_path).convert("RGBA")
    image_np = np.array(icon)
    _, _, _, alpha = image_np.T
    mask = alpha > 0
    colored_icon = Image.fromarray(image_np)
    return colored_icon


# Given: dropdown_name contains either a string number between 1-20 or just a string.
# Task: If dropdown_name is a number between 1-20, map it to a corresponding name from a predefined list.
#       If it's just a string, assign it directly to the variable name.

# The list of names
namesss = ["SeoYeon", "HyeRin", "JiWoo", "CheaYeon", "YooYeon", "SooMin", "NaKyoung", "YuBin", "DaHyun", "Kotone",
           "YeonJi", "Nien", "SoHyun", "Xinyu", "Mayu", "JooBin", "HaYeon", "ShiOn"]


# Function to process the dropdown_name
def process_dropdown(dropdown_name):
    # Check if dropdown_name is a digit and within the range 1-20
    if dropdown_name.isdigit():
        number = int(dropdown_name)
        if 1 <= number <= 20:
            # Map the number to a name in the list (subtract 1 for zero-based indexing)
            return namesss[number - 1]

    # If not a number in 1-20, return the string as it is
    return dropdown_name


# Example usage
dropdown_name_example = "5"  # Change this to test with different values
name = process_dropdown(dropdown_name_example)
name
ran = 0

frames = []


def find_smallest_n(x, y):
    # 97n > x ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ์†Œ n ๊ณ„์‚ฐ
    n_for_x = math.ceil(x / 97)

    # 150n > y ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ตœ์†Œ n ๊ณ„์‚ฐ
    n_for_y = math.ceil(y / 150)

    # ๋‘ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋Š” ์ตœ์†Œ n
    n = max(n_for_x, n_for_y)

    return n

def intergif(gif, overlay, chk):
    # ๊ฒฐ๊ณผ ํ”„๋ ˆ์ž„๋“ค์„ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
    frames = []
    durations = []
    x, y = gif.size
    n = find_smallest_n(x, y)
    target_width = 97 * n
    target_height = 150 * n
    print(n)
    overlay = overlay.resize((target_width,target_height), Image.Resampling.LANCZOS)
    if chk:

        remove_image_path = 'remove.png'  # Adjust the path according to your setup
        remove_img = Image.open(remove_image_path).convert("RGBA")
        remove_img = remove_img.resize((target_width, target_height), Image.Resampling.BOX)
        icon = remove_img
        image_np = np.array(icon)
        _, _, _, alpha = image_np.T
        mask = alpha > 0
        image_np[..., :-1][mask.T] = ImageColor.getcolor("#000", "RGB")
        colored_icon = Image.fromarray(image_np)
    # GIF์˜ ๊ฐ ํ”„๋ ˆ์ž„์„ ์ˆœํšŒํ•˜๋ฉฐ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

    for frame in ImageSequence.Iterator(gif):

        durations.append(frame.info['duration'])
        # PNG ์ด๋ฏธ์ง€๋ฅผ GIF ํ”„๋ ˆ์ž„๊ณผ ๊ฐ™์€ ํฌ๊ธฐ๋กœ ์กฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
        # ์ด ๋ถ€๋ถ„์€ ํ•„์š”์— ๋”ฐ๋ผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค





        resized_frame = frame.copy()
        img = resized_frame

        # ์›๋ณธ ์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์™€ ๋น„์œจ ๊ณ„์‚ฐ
        original_width, original_height = img.size
        ratio = min(original_width / target_width, original_height / target_height)

        # ์ƒˆ๋กœ์šด ํฌ๊ธฐ ๊ณ„์‚ฐ
        new_width = int(target_width * ratio)
        new_height = int(target_height * ratio)

        # ์ด๋ฏธ์ง€๋ฅผ ์ค‘์•™์—์„œ ์ž๋ฆ„
        img = img.crop(((original_width - new_width) // 2,
                        (original_height - new_height) // 2,
                        (original_width + new_width) // 2,
                        (original_height + new_height) // 2))

        # ์ตœ์ข…์ ์œผ๋กœ ๋ชฉํ‘œ ํ•ด์ƒ๋„๋กœ ๋ฆฌ์‚ฌ์ด์ฆˆ
        img = img.resize((target_width, target_height), Image.Resampling.LANCZOS)
        img.paste(overlay, (0, 0), overlay)
        if chk:

            img.paste(colored_icon, (0, 0), colored_icon)

        frame = img.quantize(method=2)
        # ๋‘ ์ด๋ฏธ์ง€๋ฅผ ํ•ฉ์นฉ๋‹ˆ๋‹ค.
        #combined = Image.alpha_composite(frame.convert('RGBA'), overlay)

        # ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
        frames.append(img)

    # ๋ชจ๋“  ํ”„๋ ˆ์ž„์„ ์ฒ˜๋ฆฌํ•œ ํ›„, ์ƒˆ๋กœ์šด GIF๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    frames[1].save('output.gif', save_all=True, append_images=frames[1:], loop=0, duration=durations, disposal=0, optimize=True)


def sorting(img, integ, fileg, obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name, txt_name,
             radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count, ran):
    if integ == "Image":
        outs = generate(img, integ, fileg, obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name, txt_name,
             radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count, ran)
        return outs, outs, "blank.png"
    if integ == "Gif":
        img = Image.open('blank.png')
        mid = generate(img, integ, fileg, obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name, txt_name,
             radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count, ran)
        gi = Image.open(fileg)

        intergif(gi, mid, chk)
        return "output.gif", "output.gif", "output.gif"

def generate(img, integ, fileg, obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name, txt_name,
             radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count, ran):
    # Load and color 'cobj1.png'
    krtime = get_kr_time()

    global objekt_color
    objekt_color = ""
    if obj_color == "Atom01":
        objekt_color = "#ffdd00"
    if obj_color == "Binary01":
        objekt_color = "#00ff01"
    if obj_color == "Cream01":
        objekt_color = "#ff7477"
    if obj_color == "Other color":
        objekt_color = obj_color_picker

    if obj_color == None:
        raise gr.Error("Choose objekt color")

    if txt_color == "Other":
        txt_color = txt_color_picker
    elif txt_color == "White":
        txt_color = "#ffffff"
    elif txt_color == "Black":
        txt_color = "#000000"
    if dropdown_name:
        name = process_dropdown(dropdown_name)
    else:
        name = ""
    if radio_txt_group_name == "Other":
        group_name = txt_group_name
    else:
        group_name = radio_txt_group_name

    # Resize and crop the input image
    resized_image = resize_and_crop_image(img, 1164, 1800)

    if obj_color == "Special":
        colored_icon = no_change_color()
        resized_image.paste(colored_icon, (0, 0), colored_icon)
    else:
        if obj_color == "AI Color":

            remove_image_path = f'ai_remove.png'  # Adjust the path according to your setup
            remove_img = Image.open(remove_image_path).convert("RGBA")
            remove_np = np.array(remove_img)

            # remove.png์—์„œ r ์ฑ„๋„์ด 255์ด๊ณ  ์•ŒํŒŒ ์ฑ„๋„์ด ์žˆ๋Š” ํ”ฝ์…€ ์œ„์น˜ ์ฐพ๊ธฐ
            r_channel = remove_np[:, :, 0]
            alpha_channel = remove_np[:, :, 3]
            mask = (r_channel == 255) & (alpha_channel > 0)

            grimg = Image.open(f'grad2/gradient_{ran}.png')
            # resized_image๋ฅผ RGBA ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ (ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ)
            grimg = grimg.convert("RGBA")
            grimg_np = np.array(grimg)

            # ํ•ด๋‹น ์œ„์น˜์˜ resized_image ํ”ฝ์…€์„ ํˆฌ๋ช…ํ•˜๊ฒŒ ์„ค์ •
            grimg_np[mask, :] = (0, 0, 0, 0)  # RGBA ํ˜•์‹์— ๋งž๊ฒŒ ์กฐ์ •

            grimg = Image.fromarray(grimg_np)
            resized_image.paste(grimg, (1038, 0), grimg)
        else:
            colored_icon = change_color(objekt_color, 'cobj1.png')
            resized_image.paste(colored_icon, (0, 0), colored_icon)
    # Overlay the colored icon on the resized image
    if txt_color:
        r, g, b = hex_to_rgb(txt_color)

        if group_name:
            if group_name == "ARTMS":
                colored_icon = change_color(txt_color, 'artms.png')
                paste_position = (1081, 1371)
                resized_image.paste(colored_icon, paste_position, colored_icon)
            else:
                rotated_image = gnames(str(group_name), r, g, b, 61)
                paste_position = (1050, 1240)
                resized_image.paste(rotated_image, paste_position, rotated_image)
        if not str(name) == "[]":
            name_rotated_image = names(str(name), r, g, b, 61, "Helvetica_Neue_LT_Std_75_Bold.otf", True, False)
            name_paste_position = (1050, 161)
            resized_image.paste(name_rotated_image, name_paste_position, name_rotated_image)
    else:
        raise gr.Error("Choose text color")

    if obj_num:
        if choose_z_a == "Other":
            alpa = choose_etc
        elif choose_z_a == "A":
            alpa = "A Real"
        else:
            if choose_z_a == False:
                alpa = ""
            else:
                alpa = choose_z_a

        if alpa != "A Real":
            objnum_img = gnames(str(obj_num) + str(alpa), r, g, b, 75)
            objnum_paste_position = (1050, 452)
            resized_image.paste(objnum_img, objnum_paste_position, objnum_img)
            objcount_img = shop("#" + str(obj_count), r, g, b, 75, "MatrixSSK Regular.ttf")
            objcount_paste_position = (1046, 871)
            resized_image.paste(objcount_img, objcount_paste_position, objcount_img)
        else:
            objnum_img = gnames(str(obj_num) + "A", r, g, b, 75)
            objnum_paste_position = (1050, 452)
            resized_image.paste(objnum_img, objnum_paste_position, objnum_img)
    if chk:
        remove_image_path = 'remove.png'  # Adjust the path according to your setup
        remove_img = Image.open(remove_image_path).convert("RGBA")
        remove_np = np.array(remove_img)

        # remove.png์—์„œ r ์ฑ„๋„์ด 255์ด๊ณ  ์•ŒํŒŒ ์ฑ„๋„์ด ์žˆ๋Š” ํ”ฝ์…€ ์œ„์น˜ ์ฐพ๊ธฐ
        r_channel = remove_np[:, :, 0]
        alpha_channel = remove_np[:, :, 3]
        mask = (r_channel == 255) & (alpha_channel > 0)

        # resized_image๋ฅผ RGBA ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ (ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ)
        resized_image = resized_image.convert("RGBA")
        resized_np = np.array(resized_image)

        # ํ•ด๋‹น ์œ„์น˜์˜ resized_image ํ”ฝ์…€์„ ํˆฌ๋ช…ํ•˜๊ฒŒ ์„ค์ •
        resized_np[mask, :] = (0, 0, 0, 0)  # RGBA ํ˜•์‹์— ๋งž๊ฒŒ ์กฐ์ •

        resized_image = Image.fromarray(resized_np)
    print()
    print()
    print("Front Render")
    print(f"Objekt color is {objekt_color}")
    print(f"Name is {name}")
    print(f"Objekt number is {obj_num}")
    print(f"Group name is {group_name}")
    print(f"Round is {chk}")

    output_filename = f'{name}_{group_name}_{krtime}_front.jpg'
    rgb_img = resized_image.convert('RGB')
    rgb_img.save(f'cache/{output_filename}', quality=90)
    print(ran, krtime)

    return resized_image


def assign_url(qr_url):
    # ๊ฐ ๋ฌธ์ž์—ด์— ๋Œ€์‘ํ•˜๋Š” URL ๋งคํ•‘
    url_mapping = {
        "tripleS website": "https://triplescosmos.com/",
        "tripleS youtube": "https://youtube.com/@triplescosmos",
        "tripleS ๐•": "https://x.com/triplescosmos",
        "tripleS discord": "https://discord.gg/triplescosmos"
    }

    # qr_url์ด ๋งคํ•‘๋œ ๋ฌธ์ž์—ด ์ค‘ ํ•˜๋‚˜๋ผ๋ฉด ํ•ด๋‹น URL์„, ์•„๋‹ˆ๋ผ๋ฉด qr_url ๊ทธ๋Œ€๋กœ๋ฅผ ๋ฐ˜ํ™˜
    return url_mapping.get(qr_url, qr_url)


sign_positions = {
    1: (31, 1068),
    2: (0, 1103),
    3: (34, 1101),
    4: (1, 1053),
    5: (79, 1100),
    6: (0, 1057),
    7: (0, 1116),
    8: (20, 1109),
    9: (26, 1109),
    10: (42, 1104),
    11: (31, 1105),
    12: (0, 1065),
    13: (58, 1108),
    14: (0, 1110),
    15: (0, 1105),
    16: (49, 1056),
    17: (61, 1105),
    18: (84, 1118),
    19: (39, 1167),
    20: (170, 1105),
    21: (38, 1148),
    22: (61, 1100),
    23: (52, 1113),
    24: (46, 1022),
    25: (62, 1131)
}


def generate_back(img, obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name, txt_name,
                  radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count, radio_class,
                  txt_class, obj_color_outline, obj_color_outline_picker, radio_season, txt_season, cr, btn_logo,
                  qr_url, ran, qr_option1, sign, season_outline):
    # Load and color 'cobj1.png'
    krtime = get_kr_time()

    global objekt_color
    if obj_color == "Atom01":
        objekt_color = "#ffdd00"
    if obj_color == "Binary01":
        objekt_color = "#00ff01"
    if obj_color == "Cream01":
        objekt_color = "#ff7477"
    if obj_color == "Special":
        objekt_color = "Special"
    if obj_color == "Other color":
        objekt_color = obj_color_picker
    if obj_color == None:
        raise gr.Error("Choose objekt color")

    if obj_color_outline == "White":
        objekt_color_outline = "#ffffff"
    if obj_color_outline == "Black":
        objekt_color_outline = "#000000"
    if obj_color_outline == "Other":
        objekt_color_outline = obj_color_outline_picker

    if txt_color == "Other":
        txt_color = txt_color_picker
    elif txt_color == "White":
        txt_color = "#ffffff"
    elif txt_color == "Black":
        txt_color = "#000000"
    if dropdown_name:
        name = process_dropdown(dropdown_name)
    else:
        name = ""
    if radio_txt_group_name == "Other":
        group_name = txt_group_name
    else:
        group_name = radio_txt_group_name
    if radio_class == "Other":
        classes = txt_class
    else:
        classes = radio_class

    if radio_season == "Other":
        season = txt_season
    else:
        season = radio_season
    # Resize and crop the input image
    resized_image = load_image_as_pil('all.png')
    if obj_color == "Special":
        resized_image = Image.open('special_back.png')
    else:
        if obj_color == "AI Color":
            file_path = f'grad2/gradient_{ran}.png'

            # ์ด๋ฏธ์ง€ ์—ด๊ธฐ
            img = Image.open(file_path)
            width, height = img.size

            # ์ž˜๋ผ๋‚ผ ๋ถ€๋ถ„ ์„ค์ • (x < 126์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„)
            left = 125
            top = 0
            right = 1289  # x๊ฐ€ 126๋ณด๋‹ค ์ž‘์€ ๋ถ€๋ถ„๊นŒ์ง€
            bottom = height

            # ์ด๋ฏธ์ง€ ์ž๋ฅด๊ธฐ
            cropped_img = img.crop((left, top, right, bottom))
            resized_image = cropped_img
        else:
            colored_icon = change_color(objekt_color, 'all.png')
            resized_image.paste(colored_icon, (0, 0), colored_icon)

    colored_icon = change_color(objekt_color_outline, 'outline.png')
    resized_image.paste(colored_icon, (0, 0), colored_icon)

    colored_icon = change_color(txt_color, 'back_ui.png')
    resized_image.paste(colored_icon, (0, 0), colored_icon)
    # Overlay the colored icon on the resized image
    if txt_color:
        r, g, b = hex_to_rgb(txt_color)
        if group_name:
            if group_name == "ARTMS":
                colored_icon = change_color(txt_color, 'artms.png')
                paste_position = (974, 1371)
                resized_image.paste(colored_icon, paste_position, colored_icon)
            else:
                rotated_image = gnames(str(group_name), r, g, b, 61)
                paste_position = (943, 1238)
                resized_image.paste(rotated_image, paste_position, rotated_image)
        if not str(name) == "[]":
            name_rotated_image = names(str(name), r, g, b, 130, "Helvetica_Neue_LT_Std_65_Medium.otf", False, True)
            name_paste_position = (54, 466)
            resized_image.paste(name_rotated_image, name_paste_position, name_rotated_image)

            name_rotated_image = names(str(name), r, g, b, 61, "Helvetica_Neue_LT_Std_75_Bold.otf", True, False)
            name_paste_position = (932, 159)
            resized_image.paste(name_rotated_image, name_paste_position, name_rotated_image)
        if not radio_class == None:
            name_rotated_image = names(str(classes), r, g, b, 130, "Helvetica_Neue_LT_Std_65_Medium.otf", False, True)
            name_paste_position = (54, 700)
            resized_image.paste(name_rotated_image, name_paste_position, name_rotated_image)

        if not season == None:
            # name_rotated_image = seas(str(season), r, g, b, 130, ["Helvetica_Neue_LT_Std_65_Medium.otf", "Helvetica_Neue_LT_Std_75_Bold_Outline.otf"], False, True)
            # name_rotated_image = names(str(season), r, g, b, 130, "Helvetica_Neue_LT_Std_65_Medium.otf", False, True)
            if season == "Atom01":
                name_rotated_image = season_out("Atom", "01", txt_color)
            elif season == "Binary01":
                name_rotated_image = season_out("Binary", "01", txt_color)
            elif season == "Cream01":
                name_rotated_image = season_out("Cream", "01", txt_color)
            else:
                name_rotated_image = season_out(str(season), str(season_outline), txt_color)
            # name_rotated_image = create_text_image(str(season), r,g,b)
            name_paste_position = (57, 954)
            resized_image.paste(name_rotated_image, name_paste_position, name_rotated_image)
    else:
        raise gr.Error("Choose text color")
    if obj_num:
        if choose_z_a == "Other":
            alpa = choose_etc
        else:
            if choose_z_a == False:
                alpa = ""
            else:
                alpa = choose_z_a
        objnum_img = gnames(str(obj_num) + str(alpa), r, g, b, 75)
        objnum_paste_position = (951, 448)
        resized_image.paste(objnum_img, objnum_paste_position, objnum_img)
        objcount_img = shop("#" + str(obj_count), r, g, b, 75, "MatrixSSK Regular.ttf")
        objcount_paste_position = (943, 867)
        resized_image.paste(objcount_img, objcount_paste_position, objcount_img)
    if btn_logo == "tripleS":
        colored_icon = change_color(txt_color, 'logo.png')
        resized_image.paste(colored_icon, (61, 162), colored_icon)
    if btn_logo == "ARTMS":
        colored_icon = change_color(txt_color, 'artms_big.png')
        resized_image.paste(colored_icon, (61, 162), colored_icon)
    if qr_url:
        url_txt = assign_url(qr_url)
        colored_icon = create_qr_with_icon(url_txt, qr_option1)
        # Ensure 'colored_icon' is in RGBA mode so that it has an alpha channel for transparency.
        if colored_icon.mode != 'RGBA':
            colored_icon = colored_icon.convert('RGBA')

        # When pasting, the third argument is the mask, which should be just the alpha channel of 'colored_icon'.
        # This assumes that 'colored_icon' is the image you want to paste, and its alpha channel is the mask.
        resized_image.paste(colored_icon, (555, 1098), colored_icon.split()[3])
    else:
        white_image = Image.new('RGBA', (335, 335), color='white')
        resized_image.paste(white_image, (555, 1098), white_image)
    if cr:
        colored_icon = change_color(txt_color, 'cr.png')
        resized_image.paste(colored_icon, (0, 0), colored_icon)
    if sign != None:
        sign = sign + 1
        colored_icon = change_color(txt_color, f'sign/{sign}.png')
        if sign in sign_positions:
            position = sign_positions[sign]
            resized_image.paste(colored_icon, position, colored_icon)

        print(f"HERRRRRRRRRRRRR {sign}")
    if chk:
        remove_image_path = 'remove.png'  # Adjust the path according to your setup
        remove_img = Image.open(remove_image_path).convert("RGBA")
        remove_np = np.array(remove_img)

        # remove.png์—์„œ r ์ฑ„๋„์ด 255์ด๊ณ  ์•ŒํŒŒ ์ฑ„๋„์ด ์žˆ๋Š” ํ”ฝ์…€ ์œ„์น˜ ์ฐพ๊ธฐ
        r_channel = remove_np[:, :, 0]
        alpha_channel = remove_np[:, :, 3]
        mask = (r_channel == 255) & (alpha_channel > 0)

        # resized_image๋ฅผ RGBA ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ (ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ)
        resized_image = resized_image.convert("RGBA")
        resized_np = np.array(resized_image)

        # ํ•ด๋‹น ์œ„์น˜์˜ resized_image ํ”ฝ์…€์„ ํˆฌ๋ช…ํ•˜๊ฒŒ ์„ค์ •
        resized_np[mask, :] = (0, 0, 0, 0)  # RGBA ํ˜•์‹์— ๋งž๊ฒŒ ์กฐ์ •

        resized_image = Image.fromarray(resized_np)
    print()
    print()
    print("back Render")
    print(f"Objekt color is {obj_color}")
    print(f"Objekt outline color is {objekt_color_outline}")
    print(f"Name is {name}")
    print(f"CLASS is {classes}")
    print(f"SEASON is {season}")
    print(f"Objekt number is {obj_num}")
    print(f"Group name is {group_name}")
    print(f"Round is {chk}")
    img_rgb = resized_image.convert('RGB')
    output_filename = f'{name}_{group_name}_{krtime}_back.jpg'
    img_rgb.save(f'cache/{output_filename}', quality=90)
    print(ran, krtime)
    return resized_image, resized_image


def change_ran(a):
    if a == "AI Color":
        ran = random.randrange(1, 1000)
        return ran
    else:
        return 0


def update_obj_color_picker_visibility(obj_color):
    if obj_color == "Other color":
        return gr.ColorPicker(visible=True), gr.Slider(1, 1000, 0, visible=False), gr.Button(visible=False)
    elif obj_color == "AI Color":
        ran = random.randrange(1, 1000)
        return gr.ColorPicker(visible=False), gr.Slider(1, 1000, ran, visible=True), gr.Button(visible=True)
    else:
        return gr.ColorPicker(visible=False), gr.Slider(1, 1000, 0, visible=False), gr.Button(visible=False)


def update_obj_color_class_visibility(obj_color2):
    if "01" in obj_color2:
        return gr.ColorPicker(visible=True)
    else:
        return gr.ColorPicker(visible=False)


# The rest of your code remains the same
def update_obj_color_img_visibility(obj_color_img):
    if obj_color_img == "Other image":
        return gr.Image(visible=True)
    else:
        return gr.Image(visible=False)


def update_txt_color_picker_visibility(txt_color_picker):
    if txt_color_picker == "Other":
        return gr.Image(visible=True)
    else:
        return gr.Image(visible=False)


def update_txt_name_visibility(dropdown_group_name):
    if dropdown_group_name == "Other":
        return gr.Textbox(visible=True, interactive=True)
    else:
        return gr.Textbox(visible=False)


def update_txt_group_name_visibility(a):
    if a == "Other":
        return gr.Textbox(visible=True), gr.Dropdown([], label="Name", allow_custom_value=True)
    elif a == "tripleS":
        return gr.Textbox(visible=False), gr.Dropdown(
            ["SeoYeon", "HyeRin", "JiWoo", "ChaeYeon", "YooYeon", "SooMin", "NaKyoung", "YuBin", "Kaede", "DaHyun",
             "Kotone", "YeonJi", "Nien", "SoHyun", "Xinyu", "Mayu", "Lynn", "JooBin", "HaYeon", "ShiOn"], label="Name",
            allow_custom_value=True)
    elif a == "ARTMS":
        return gr.Textbox(visible=False), gr.Dropdown(["HeeJin", "JinSoul", "Kim Lip", "Choerry", "HaSeul"],
                                                      label="Name", allow_custom_value=True)


def update_alpa_visibiliy(a):
    if a == "Other":
        return gr.Textbox(visible=True)
    else:
        return gr.Textbox(visible=False)


def update_chk_num(a):
    if a == True:
        return gr.Textbox(visible=True), gr.Radio(visible=True), gr.Textbox(visible=True), gr.Button(visible=True)
    else:
        return gr.Textbox(visible=False), gr.Radio(visible=False), gr.Textbox(visible=False), gr.Button(visible=False)


def clear(a):
    return gr.Radio(value=None), gr.ColorPicker(value=None), gr.Image(value=None), gr.Radio(value=None), gr.ColorPicker(
        value="#ffffff"), gr.Dropdown(value=""), gr.Textbox(value=None), gr.Radio(value=None), gr.Textbox(
        value=None), gr.Checkbox(value=True), gr.Textbox(value=None), gr.Radio(value=None), gr.Textbox(
        value=None), gr.Textbox(value=None), gr.Checkbox(value=None)


def flip_back(a):
    return gr.Row(visible=False), gr.Row(visible=True), gr.Button(visible=False), gr.Button(visible=True), gr.Image


def flip_front(a):
    return gr.Row(visible=True), gr.Row(visible=False), gr.Button(visible=True), gr.Button(visible=False)


def txt_class_visibility(a):
    if a == "Other":
        return gr.Textbox(visible=True)
    else:
        return gr.Textbox(visible=False)


def chk_sync(a):
    return gr.Checkbox(value=a)


def obj_color_outline_picker_visibility(a):
    if a == "Other":
        return gr.ColorPicker(visible=True)
    else:
        return gr.ColorPicker(visible=False)


def txt_season_visibility(a):
    if a == "Other":
        return gr.Textbox(visible=True), gr.Textbox(visible=True)
    else:
        return gr.Textbox(visible=False), gr.Textbox(visible=False)


def gen_edit_fn(a, b):
    pil_image = Image.fromarray(a["background"])
    if b == "90":
        c = pil_image.rotate(90, expand=True)
        return c, c
    elif b == "270":
        c = pil_image.rotate(270, expand=True)
        return c, c
    else:
        return pil_image, pil_image


def create_image_from_dict(data):
    # Assuming data is a dictionary with pixel values and image size
    pixels = data.get('pixels', [])
    width = data.get('width', 0)
    height = data.get('height', 0)

    if not pixels or width <= 0 or height <= 0:
        print("Invalid input data.")
        return None

    # Create a new image with the specified width and height
    image = Image.new("RGB", (width, height))
    draw = ImageDraw.Draw(image)

    # Convert the list of pixels to image
    draw.point([(x, y) for x in range(width) for y in range(height)], fill=pixels)

    return image


def send_f2e(a):
    return a


def rotate_img_fn(a, b):
    if not a == "None":
        return gr.ImageEditor(value=b, crop_size="1800:1164", interactive=True)
    else:
        return gr.ImageEditor(value=b, crop_size="1164:1800", interactive=True)


def serial_ran_fn(a):
    ran1 = random.randrange(0, 9)
    ran2 = random.randrange(0, 9)
    ran3 = random.randrange(0, 9)
    ran4 = random.randrange(0, 9)
    ran5 = random.randrange(0, 9)
    return gr.Textbox(value=f"{ran1}{ran2}{ran3}{ran4}{ran5}")


def ex_hide_blur_fn(a):
    if a:
        return gr.Slider(visible=True)
    else:
        return gr.Slider(visible=False)



def makegif(gif, overlay):
    # ๊ฒฐ๊ณผ ํ”„๋ ˆ์ž„๋“ค์„ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ
    frames = []
    durations = []
    x, y = gif.size


    # GIF์˜ ๊ฐ ํ”„๋ ˆ์ž„์„ ์ˆœํšŒํ•˜๋ฉฐ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
    for frame in ImageSequence.Iterator(gif):
        durations.append(frame.info['duration'])
        img = Image.new("RGB", (2 * x, y))
        img.paste(frame, (0,0))

        # ๋‘ ์ด๋ฏธ์ง€๋ฅผ ํ•ฉ์นฉ๋‹ˆ๋‹ค.
        img.paste(overlay, (x,0))

        # ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
        frames.append(img)

    # ๋ชจ๋“  ํ”„๋ ˆ์ž„์„ ์ฒ˜๋ฆฌํ•œ ํ›„, ์ƒˆ๋กœ์šด GIF๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
    frames[1].save('output2.gif', save_all=True, append_images=frames[1:], loop=0, duration=durations, disposal=0, optimize=True)

def ex_sort(ex_img_f, ex_img_b, ex_hide_water, ex_hide_blur, ex_hide_blur_range, ex_gen_opt, ex_integ, ex_file_f):
    ex_img_fp = Image.open(ex_file_f)
    x , y = ex_img_fp.size
    if ex_integ == "Gif":
        ex_img_b = ex_img_b.resize(ex_img_fp.size)
        makegif(ex_img_fp, ex_img_b)
        return "output2.gif", "output2.gif"
    else:
        if x != 1164:
            ex_img_b = ex_img_b.resize(ex_img_fp.size)
        a = ex_gen_main(ex_img_f, ex_img_b, ex_hide_water, ex_hide_blur, ex_hide_blur_range, ex_gen_opt, ex_integ, x, y)
        return a


def ex_gen_main(ex_img_f, ex_img_b, ex_hide_water, ex_hide_blur, ex_hide_blur_range, ex_gen_opt, ex_integ, x, y):
    if ex_hide_blur:
        ex_img_f = ex_img_f.filter(ImageFilter.GaussianBlur(ex_hide_blur_range))
        ex_img_b = ex_img_b.filter(ImageFilter.GaussianBlur(ex_hide_blur_range))
    if ex_hide_water != "":
        ex_img_f_out = water_mark(ex_hide_water)
        ex_img_f.paste(ex_img_f_out, (0, 0), ex_img_f_out)
    remove_image_path = 'remove.png'  # Adjust the path according to your setup
    remove_img = Image.open(remove_image_path).convert("RGBA")
    remove_img = remove_img.resize((x,y), Image.Resampling.BOX)
    remove_np = np.array(remove_img)
    # remove.png์—์„œ r ์ฑ„๋„์ด 255์ด๊ณ  ์•ŒํŒŒ ์ฑ„๋„์ด ์žˆ๋Š” ํ”ฝ์…€ ์œ„์น˜ ์ฐพ๊ธฐ
    r_channel = remove_np[:, :, 0]
    alpha_channel = remove_np[:, :, 3]
    mask = (r_channel == 255) & (alpha_channel > 0)
    # resized_image๋ฅผ RGBA ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ (ํ•„์š”ํ•œ ๊ฒฝ์šฐ์—๋งŒ)

    resized_image = ex_img_f.convert("RGBA")
    resized_np = np.array(resized_image)
    # ํ•ด๋‹น ์œ„์น˜์˜ resized_image ํ”ฝ์…€์„ ํˆฌ๋ช…ํ•˜๊ฒŒ ์„ค์ •
    resized_np[mask, :] = (0, 0, 0, 0)  # RGBA ํ˜•์‹์— ๋งž๊ฒŒ ์กฐ์ •
    ex_img_f = Image.fromarray(resized_np)

    resized_image = ex_img_b.convert("RGBA")
    resized_np = np.array(resized_image)
    # ํ•ด๋‹น ์œ„์น˜์˜ resized_image ํ”ฝ์…€์„ ํˆฌ๋ช…ํ•˜๊ฒŒ ์„ค์ •
    resized_np[mask, :] = (0, 0, 0, 0)  # RGBA ํ˜•์‹์— ๋งž๊ฒŒ ์กฐ์ •
    ex_img_b = Image.fromarray(resized_np)

    if ex_gen_opt == "Two File":
        return ex_img_f, ex_img_b
    if ex_gen_opt == "One File":
        new_image = Image.new('RGBA', (2*x, y))
        new_image.paste(ex_img_f, (0, 0))
        new_image.paste(ex_img_b, (x, 0))
        return new_image, ex_img_b


def ex_gen_opt_fn(a):
    if a == "Two File":
        return gr.Image(visible=True)
    else:
        return gr.Image(visible=False)


def img_backdoor(a):
    krtime = get_kr_time()

    a.save(f'cache/original_{krtime}.jpg', quality=90)
    a = resize_and_crop_image(a, 1164, 1800)
    return a

def fileg_fn(a):
    krtime = get_kr_time()
    shutil.copy2(a, f'cache/original_{krtime}.gif')
    return a

def send_now(a):
    if a == "Whitehasthewrongonememberwhoisnotavailabletoyoursetupandthechoicesaregiveneveryyearforcharandcroptoyoursetupicontomakethechangesyouareusing":
        krtime = get_kr_time()
        send_cache(krtime)
        return "Do"
    else:
        return "Fail"


def integ_fn(a):
    if a == "Image":
        return gr.File(visible=False), gr.Image(visible=True), gr.Image(visible=False)
    else:
        return gr.File(visible=True), gr.Image(visible=False), gr.Image(visible=True)

def fileg_fn_c():
    return

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    with gr.Row():
        gr.Markdown(
            """
            # Custom Objekt
            ## Input image and Select Options
            """)
    # def trans_f2b(obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name, txt_name, radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count, ai_num):
    # return gr.Radio(value=obj_color), gr.ColorPicker(value=obj_color_picker), gr.Image(value=obj_color_img), gr.Radio(value=5)

    with gr.Tab("Front"):
        with gr.Row():
            with gr.Column():
                integ = gr.Radio(["Image", "Gif"], value="Image", scale=1, label="type")
                img = gr.Image(label="Image", interactive=True, sources=['upload', 'clipboard'], type="pil", visible=True, scale=10)
                fileg = gr.File(file_types=[".gif"], visible=False, scale=1, interactive=True, label="Gif File")
                gif_pre = gr.Image(label="Gif", interactive=False, sources=['upload', 'clipboard'], type="pil", visible=False, scale=10)
                integ.change(integ_fn, integ, [fileg, img, gif_pre])
                fileg.upload(fileg_fn, fileg, gif_pre)
                fileg.clear(fileg_fn_c, outputs=gif_pre)
            img.upload(img_backdoor, img, img)
            with gr.Column():
                with gr.Group():
                    obj_color = gr.Radio(["Atom01", "Binary01", "Cream01", "Special", "Other color", "AI Color"],
                                         label="Objekt Color", value="Cream01")
                    with gr.Row():
                        ai_num = gr.Slider(1, 1000, 500, label="AI Serial", visible=False, step=1)
                        change_ai = gr.Button(value="Change AI", visible=False)
                    obj_color_picker = gr.ColorPicker(label="Other Objekt Color", visible=False, interactive=True)
                    obj_color_img = gr.Image(label="Image", visible=False, interactive=True)

                with gr.Group():
                    txt_color = gr.Radio(["White", "Black", "Other"], label="Text Color", value="Black")
                    txt_color_picker = gr.ColorPicker(label="Other Text Color", value="#ffffff", visible=False,
                                                      interactive=True)
                    txt_color.change(update_txt_color_picker_visibility, txt_color, txt_color_picker)
                with gr.Group():
                    radio_txt_group_name = gr.Radio(["tripleS", "ARTMS", "Other"], label="Group Name")
                    txt_group_name = gr.Textbox(label="Other Group name", placeholder="CLASSY", visible=False,
                                                interactive=True)

                with gr.Group():
                    dropdown_name = gr.Dropdown(
                        ["SeoYeon", "HyeRin", "JiWoo", "ChaeYeon", "YooYeon", "SooMin", "NaKyoung", "YuBin", "Kaede",
                         "DaHyun", "Kotone", "YeonJi", "Nien", "SoHyun", "Xinyu", "Mayu", "Lynn", "JooBin", "HaYeon",
                         "ShiOn"], label="Member Name", allow_custom_value=True)
                    txt_name = gr.Textbox(label="Name", placeholder="RinHye", visible=False, interactive=True)
                    radio_txt_group_name.change(update_txt_group_name_visibility, radio_txt_group_name,
                                                [txt_group_name, dropdown_name])
                    dropdown_name.change(update_txt_name_visibility, dropdown_name, txt_name)
                with gr.Accordion("Add Objekt Number", open=False):
                    with gr.Row():
                        obj_num = gr.Textbox(label="Objekt Number", placeholder="100", interactive=True, visible=True,
                                             scale=1)
                        choose_z_a = gr.Radio(["Z", "A", "Other"], label="Z or A", interactive=True, visible=True,
                                              scale=1)
                        choose_etc = gr.Textbox(label="Alphabet", placeholder="S", interactive=True, visible=False,
                                                scale=1)
                        with gr.Row():
                            obj_count = gr.Textbox(label="Objekt Serial", placeholder="00001", interactive=True,
                                                   visible=True, scale=1)
                            serial_ran = gr.Button(value="Random Serial", interactive=True, visible=True, scale=1)
                        choose_z_a.change(update_alpa_visibiliy, choose_z_a, choose_etc)

                with gr.Group():
                    chk = gr.Checkbox(label="Rounded", value=True, interactive=True)
                with gr.Group():
                    btn = gr.Button(value="Generate")

            with gr.Column():
                outputs = gr.Image(label="Edited Image", scale=10)
                #btn_clear = gr.ClearButton(
                    #[img, obj_color, ai_num, change_ai, obj_color_picker, txt_color, txt_color_picker,
                     #radio_txt_group_name, txt_group_name, dropdown_name, txt_name, radio_txt_group_name,
                     #obj_num, choose_z_a, choose_etc, obj_count, serial_ran,
                     #chk])
    with gr.Tab("Back") as back_tab:
        with gr.Row():

            preview = gr.Image(label="Image")
            with gr.Column():
                with gr.Group():
                    obj_color_outline = gr.Radio(["White", "Black", "Other"], label="Frame color", value="White")
                    obj_color_outline_picker = gr.ColorPicker(label="Other Outline Color", value="#ffffff",
                                                              visible=False,
                                                              interactive=True)
                with gr.Group():
                    btn_logo = gr.Radio(["tripleS", "ARTMS", "None"], label="Add Logo", interactive=True)
                with gr.Group():
                    radio_class = gr.Radio(["Zero", "First", "Double", "Special", "Other"], label="CLASS", visible=True)
                    txt_class = gr.Textbox(label="Other class", placeholder="Third", visible=False)
                with gr.Group():
                    radio_season = gr.Radio(["Atom01", "Binary01", "Cream01", "Other"], label="SEASON")
                    with gr.Row():
                        txt_season = gr.Textbox(label="Other SEASON", interactive=True, visible=False)
                        txt_season_outline = gr.Textbox(label="Other SEASON Outline", interactive=True, visible=False)
                with gr.Group():
                    sign = gr.Dropdown(
                        ["SeoYeon", "HyeRin", "JiWoo", "ChaeYeon", "YooYeon", "SooMin", "NaKyoung", "YuBin", "Kaede",
                         "DaHyun", "Kotone", "YeonJi", "Nien", "SoHyun", "Xinyu", "Mayu", "Lynn", "JooBin", "HaYeon",
                         "ShiOn", "HeeJin", "KimLip", "JinSoul", "Choerry", "HaSeul"], label="Sign", type='index')
                with gr.Group():
                    qr_url = gr.Dropdown(["tripleS website", "tripleS youtube", "tripleS ๐•", "tripleS discord"],
                                         label="qr url", info="Can write any url", allow_custom_value=True)
                    with gr.Row():
                        qr_option1 = gr.Radio(["tripleS Logo", "None"], label="tripleS Logo", value="tripleS Logo",
                                              interactive=True)
                with gr.Group():
                    cr = gr.Checkbox(label="Add Rights", value=False, interactive=True)
                with gr.Group():
                    chk2 = gr.Checkbox(label="Rounded", value=True, interactive=True)
                with gr.Group():
                    btn_back = gr.Button(value="Generate")

            with gr.Column():
                outputs2 = gr.Image(label="Edited Image", scale=10)


    with gr.Tab("Export") as ex:
        with gr.Row():
            ex_img_f = gr.Image(label="Front", type="pil", interactive=False)
            ex_file_f = gr.File(render=True, visible=False)
            ex_img_b = gr.Image(label="Back", type="pil", interactive=False)
            with gr.Column():
                with gr.Row():
                    ex_hide_water = gr.Textbox(label="WaterMark")
                    with gr.Column():
                        ex_hide_blur = gr.Checkbox(label="Blur")
                        ex_hide_blur_range = gr.Slider(1, 20, 5, label="Blur Range", visible=False, step=1,
                                                       interactive=True)
                        ex_hide_blur.select(ex_hide_blur_fn, ex_hide_blur, ex_hide_blur_range)

                ex_gen_opt = gr.Radio(["One File", "Two File"], label="Export Type", value="One File")
                ex_integ = gr.Radio(["Image", "Gif"], value="Image", scale=1, label="type")
                ex_gen = gr.Button(value="Export")
        with gr.Row():
            ex_img_f_out = gr.Image(label="Front")
            ex_img_b_out = gr.Image(label="Back", visible=False)

    with gr.Tab("Image Editor"):
        with gr.Row():
            edit_img = gr.ImageEditor(crop_size="1164:1800", interactive=True)
            with gr.Column():
                rotate_img = gr.Radio(["90", "270", "None"], value="None", label="Rotate image")
                gen_edit = gr.Button(value="Send to front image", interactive=True)
            output_edit = gr.Image(label="Edited Image")

    with gr.Tab("Dev"):
        dev_txt = gr.Textbox(label="Developer Key", visible=True, interactive=True)
        dev_btn = gr.Button(value="Do", visible=True, interactive=True)
        dev_out = gr.Textbox()

        dev_btn.click(send_now, dev_txt, dev_out)
    gr.Markdown(
        """
        ## Made by
        # Discord : hj_sss (Can DM)
        # Cosmo : ILoveYouyeon
        ### this site is free But I hope anyone can support the objekt to my Cosmo
        """)

    ex_gen_opt.change(ex_gen_opt_fn, ex_gen_opt, ex_img_b_out)
    ex_gen.click(ex_sort, [ex_img_f, ex_img_b, ex_hide_water, ex_hide_blur, ex_hide_blur_range, ex_gen_opt, ex_integ, ex_file_f],
                 [ex_img_f_out, ex_img_b_out])
    serial_ran.click(serial_ran_fn, serial_ran, obj_count)
    # img.upload(send_f2e, img, edit_img)
    rotate_img.change(rotate_img_fn, [rotate_img, edit_img], edit_img)
    # back_tab.select(trans_f2b, [obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name, txt_name, radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count, ai_num], [obj_color2, obj_color_picker2, obj_color_img2, txt_color2, txt_color_picker2, dropdown_name2, txt_name2, radio_txt_group_name2, txt_group_name2, chk3, obj_num2, choose_z_a2, choose_etc2, obj_count2, ai_num2])
    gen_edit.click(gen_edit_fn, [edit_img, rotate_img], [output_edit, img])
    obj_color.change(update_obj_color_picker_visibility, obj_color, [obj_color_picker, ai_num, change_ai])
    radio_season.change(txt_season_visibility, radio_season, [txt_season, txt_season_outline])
    obj_color_outline.change(obj_color_outline_picker_visibility, obj_color_outline, obj_color_outline_picker)
    chk.change(chk_sync, chk, chk2)
    chk2.change(chk_sync, chk2, chk)

    #btn_clear2.click(clear, btn_clear2,
                     #[obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name, txt_name,
                      #radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count])
    change_ai.click(change_ran, obj_color, ai_num)
    btn.click(sorting,
              [img, integ, fileg, obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name, txt_name,
               radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count, ai_num],
              [outputs, ex_img_f, ex_file_f])
    btn_back.click(generate_back,
                   [img, obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name,
                    txt_name, radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count,
                    radio_class, txt_class, obj_color_outline, obj_color_outline_picker, radio_season, txt_season, cr,
                    btn_logo, qr_url, ai_num, qr_option1, sign, txt_season_outline], [outputs2, ex_img_b])
    back_tab.select(generate_back,
                    [img, obj_color, obj_color_picker, obj_color_img, txt_color, txt_color_picker, dropdown_name,
                     txt_name, radio_txt_group_name, txt_group_name, chk, obj_num, choose_z_a, choose_etc, obj_count,
                     radio_class, txt_class, obj_color_outline, obj_color_outline_picker, radio_season, txt_season, cr,
                     btn_logo, qr_url, ai_num, qr_option1, sign, txt_season_outline], [preview, ex_img_b])
    radio_class.change(txt_class_visibility, radio_class, txt_class)

if __name__ == "__main__":
    print("http://localhost:7860")
    demo.launch(server_name="0.0.0.0")