File size: 34,284 Bytes
3ad29d4 a00e3e3 e63795d f455e13 f39920b d2ec453 f59da55 138ec60 f39920b 3ad29d4 f39920b 3ad29d4 f39920b 62466fb f59da55 412fdc1 62466fb 412fdc1 f59da55 a00e3e3 3ad29d4 a00e3e3 3ad29d4 a00e3e3 3ad29d4 a00e3e3 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 b8ca94f 98f2f4c b8ca94f f59da55 b8ca94f 98f2f4c b8ca94f 98f2f4c b8ca94f 98f2f4c 8ea7398 98f2f4c b8ca94f 98f2f4c b8ca94f 8ea7398 5ae439a f692ec1 a1b01cf 5d3bf84 f59da55 5d3bf84 a1b01cf 5d3bf84 a1b01cf 5d3bf84 3ad29d4 f59da55 3ad29d4 02334ae f59da55 3ad29d4 02334ae 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 412fdc1 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 cecda84 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 02334ae 5b7845b 02334ae f39920b cecda84 3ad29d4 a00e3e3 3ad29d4 a00e3e3 f59da55 02334ae f455e13 02334ae a00e3e3 02334ae f59da55 f455e13 f59da55 cecda84 f59da55 cecda84 f59da55 cecda84 f59da55 cecda84 f59da55 02334ae cecda84 f51d222 cecda84 02334ae f59da55 02334ae f59da55 3e8da91 f59da55 02334ae f59da55 02334ae f59da55 02334ae a00e3e3 02334ae cecda84 02334ae 5b7845b 02334ae f39920b cecda84 02334ae cecda84 3ad29d4 c5f1675 f59da55 c5f1675 3ad29d4 c5f1675 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 f59da55 02334ae f59da55 02334ae f59da55 02334ae f59da55 02334ae f59da55 02334ae f59da55 02334ae 62466fb 3ad29d4 a00e3e3 8a0e438 3ad29d4 02334ae 3ad29d4 f59da55 c5f1675 3ad29d4 f59da55 3ad29d4 02334ae f59da55 3ad29d4 f59da55 3ad29d4 f59da55 3ad29d4 02334ae c5f1675 02334ae f59da55 a00e3e3 02334ae a00e3e3 f59da55 f455e13 a00e3e3 02334ae cecda84 02334ae f59da55 cecda84 02334ae cecda84 02334ae 3ad29d4 f59da55 3ad29d4 |
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 |
import gradio as gr
import numpy as np
from PIL import Image, ImageDraw, ImageFont, ImageColor
import os
import qrcode
from datetime import datetime
import pytz
import paramiko
import io
import re
import random
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_pil_image_to_sftp(image, remote_image_name):
host = "192.168.219.113"
port = 22
username = "hj6ch"
password = "mine06^^"
remote_path = f"/home/hj6ch/outputs/{remote_image_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)
# Open the icon file
icon_img = Image.open(icon_path)
# 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')
# ์๋ณธ ์ด๋ฏธ์ง์ ํฌ๊ธฐ์ ๋น์จ ๊ณ์ฐ
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
def generate(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, ran):
# Load and color 'cobj1.png'
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 image":
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
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 = (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)
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}")
krtime = get_kr_time()
output_filename = f'{name}_{group_name}_{krtime}_front.png'
print(ran)
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)
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):
# Load and color 'cobj1.png'
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 image":
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)
# name_rotated_image = create_text_image(str(season), r,g,b)
name_paste_position = (56, 939)
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:
colored_icon = change_color(txt_color, 'logo.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_icon.png')
# 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])
if cr:
colored_icon = change_color(txt_color, 'cr.png')
resized_image.paste(colored_icon, (0, 0), colored_icon)
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}")
krtime = get_kr_time()
output_filename = f'{name}_{group_name}_{krtime}_back.png'
print(ran)
return resized_image
def change_ran(a):
if a == "AI image":
ran = random.randrange(1, 64)
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, 64, 0, label="AI id", visible=False), gr.Button(visible=False)
elif obj_color == "AI image":
ran = random.randrange(1, 64)
return gr.ColorPicker(visible=False), gr.Slider(1, 64, ran, label="AI id", visible=True), gr.Button(visible=True)
else:
return gr.ColorPicker(visible=False), gr.Slider(1, 64, 0, label="AI id", 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)
else:
return gr.Textbox(visible=False)
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)
else:
return gr.Textbox(visible=False), gr.Radio(visible=False), gr.Textbox(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)
else:
return gr.Textbox(visible=False)
with gr.Blocks(theme=gr.themes.Soft()) as demo:
with gr.Row():
gr.Markdown(
"""
# Custom Objekt
## Input image and Select Options
""")
f_btn_back = gr.Button(value="Flip", scale=1, visible=True)
f_btn_front = gr.Button(value="Flip", scale=1, visible=False)
with gr.Row(visible=True) as al1:
img = gr.Image(label="Image")
with gr.Column():
with gr.Group():
obj_color = gr.Radio(["Atom01", "Binary01", "Cream01", "Special", "Other color", "AI image"],
label="Objekt Color", value="Cream01")
with gr.Row():
ai_num = gr.Slider(1, 64, 10, label="AI id", 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)
obj_color.change(update_obj_color_img_visibility, obj_color, obj_color_img)
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():
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="Name", allow_custom_value=True)
txt_name = gr.Textbox(label="Name", placeholder="RinHye", visible=False, interactive=True)
dropdown_name.change(update_txt_name_visibility, dropdown_name, txt_name)
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)
radio_txt_group_name.change(update_txt_group_name_visibility, radio_txt_group_name, txt_group_name)
with gr.Group():
with gr.Row():
obj_num = gr.Textbox(label="Objekt number", placeholder="100", interactive=True, visible=False)
choose_z_a = gr.Radio(["Z", "A", "Other"], label="Z or A", interactive=True, visible=False)
choose_etc = gr.Textbox(label="Alphabet", placeholder="S", interactive=True, visible=False)
obj_count = gr.Textbox(label="Objekt Serial", placeholder="00001", interactive=True, visible=False)
choose_z_a.change(update_alpa_visibiliy, choose_z_a, choose_etc)
chk_num = gr.Checkbox(label="Add Objekt numbers")
chk_num.change(update_chk_num, chk_num, [obj_num, choose_z_a, obj_count])
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(value="Clear")
with gr.Row(visible=False) as al2:
preview = gr.Image(label="Image")
with gr.Column():
with gr.Group():
obj_color_outline = gr.Radio(["White", "Black", "Other"], label="Out line", value="White")
obj_color_outline_picker = gr.ColorPicker(label="Other Outline Color", value="#ffffff", visible=False,
interactive=True)
with gr.Group():
btn_logo = gr.Checkbox(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")
txt_season = gr.Textbox(label="Other SEASON", interactive=True, visible=False)
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.Checkbox(label="tripleS logo", value=False, interactive=True)
qr_option2 = gr.Checkbox(label="ARTMS (Cosmo) logo", value=False, 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)
btn_clear2 = gr.ClearButton(value="Clear")
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)
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_clear.click(clear, btn_clear,
[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, chk_num])
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, chk_num])
change_ai.click(change_ran, obj_color, ai_num)
btn.click(generate, [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, ai_num], outputs)
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], outputs2)
radio_class.change(txt_class_visibility, radio_class, txt_class)
f_btn_back.click(flip_back, f_btn_back, [al1, al2, f_btn_back, f_btn_front])
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], preview)
f_btn_front.click(flip_front, f_btn_back, [al1, al2, f_btn_back, f_btn_front])
if __name__ == "__main__":
demo.launch()
|