gojiteji commited on
Commit
00b57c1
1 Parent(s): 6a9c941

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +216 -0
app.py ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ from rembg import remove
3
+ import cairosvg
4
+ import io
5
+ import numpy as np
6
+ from sklearn.cluster import KMeans
7
+ from PIL import Image, ImageDraw, ImageFont
8
+ import random
9
+
10
+ def generate(logo, Vtubername):
11
+ def extract_dominant_colors(img, num_colors=3, ignore_edges=True):
12
+ if img.mode == 'RGBA':
13
+ image = img.convert('RGB')
14
+ else:
15
+ image = img
16
+
17
+ image = image.resize((150, 150))
18
+ data = np.array(image)
19
+
20
+ pixels = data.reshape(-1, 3)
21
+
22
+ if ignore_edges:
23
+ edge_pixels = np.concatenate([data[0, :, :], data[-1, :, :], data[:, 0, :], data[:, -1, :]], axis=0)
24
+ edge_colors, counts = np.unique(edge_pixels, axis=0, return_counts=True)
25
+ background_color = edge_colors[counts.argmax()]
26
+ pixels = pixels[~np.all(pixels == background_color, axis=1)]
27
+ if len(pixels) == 0:
28
+ return np.array([background_color,np.array([60,60,60]),np.array([255,255,255])])
29
+ elif len(pixels) == 1:
30
+ return np.array([pixels[0],np.array([60,60,60]),np.array([255,255,255])])
31
+ elif len(pixels) == 2:
32
+ return np.array([pixels[0],pixels[1],np.array([60,60,60])])
33
+
34
+
35
+
36
+ model = KMeans(n_clusters=3)
37
+ model.fit(pixels)
38
+
39
+ colors = model.cluster_centers_
40
+
41
+ colors = colors.round(0).astype(int)
42
+
43
+ return colors
44
+
45
+ # 使用例
46
+ dominant_colors = extract_dominant_colors(logo, num_colors=3)
47
+ template_prime_colors = {
48
+ "black color": [0, 0, 0],
49
+ "white": [255, 255, 255],
50
+ "red": [255, 0, 0],
51
+ "lightgreen": [0, 255, 0],
52
+ "blue": [0, 0, 255],
53
+ "yellow": [255, 255, 0],
54
+ "mustard color": [255, 204, 34],
55
+ "lightblue": [0, 255, 255],
56
+ "pink": [255, 0, 255],
57
+ "gray": [128, 128, 128],
58
+ "maroon": [128, 0, 0],
59
+ "olive": [128, 128, 0],
60
+ "green": [0, 128, 0],
61
+ "purple": [128, 0, 128],
62
+ "navy": [0, 0, 128],
63
+ "orange": [255, 165, 0],
64
+ "bluegreen": [0, 128, 128],
65
+ "lightpurple": [128, 128, 255],
66
+ "skyblue color": [0, 128, 255],
67
+ "brown": [139,69,19],
68
+ }
69
+ _primary_color = dominant_colors[0]
70
+ # get closest color name
71
+ closest_color = "black color"
72
+ for color in template_prime_colors:
73
+ if np.linalg.norm(np.array(template_prime_colors[color]) - _primary_color) < np.linalg.norm(np.array(template_prime_colors[closest_color]) - _primary_color):
74
+ closest_color = color
75
+ primary_color = closest_color
76
+ print(primary_color)
77
+ secondary_color=str("rgb("+str(dominant_colors[1][0])+", "+str(dominant_colors[1][1])+", "+str(dominant_colors[1][2])+")")
78
+ third_color=str("rgb("+str(dominant_colors[2][0])+", "+str(dominant_colors[2][1])+", "+str(dominant_colors[2][2])+")")
79
+ exit()
80
+ import requests
81
+ import os
82
+ sdkey = os.environ["sdkey"]
83
+
84
+ response = requests.post(
85
+ f"https://api.stability.ai/v2beta/stable-image/generate/sd3",
86
+ headers={
87
+ "authorization": f"Bearer "+sdkey,
88
+ "accept": "image/*"
89
+ },
90
+ files={"none": ''},
91
+ data={
92
+ "model": "sd3",
93
+ "prompt": "pop sweety cute kawaii font anime title logo drawn by adobe illustorator. Logo for kids amime. The title logo text is \""+Vtubername+"\""+", The logo text color:"+primary_color + ". Single Logo only.",
94
+ "negative_prompt": "subtitle,face, ruby text, subscript, superscript, multiple titles, character, ugly, blurry, dirty, character face, face, watermark, low res, cropped, worst quality, jpeg artifacts, , picture frame, out of frame,animal, person face, low-res, blurry, blur, out of focus, disgusting",
95
+ "output_format": "jpeg",
96
+ },
97
+ )
98
+ image = None
99
+ if response.status_code == 200:
100
+ image = response.content
101
+ else:
102
+ raise Exception(str(response.json()))
103
+ image = Image.open(io.BytesIO(response.content))
104
+ title_logo=remove(image)
105
+
106
+
107
+ from huggingface_hub import InferenceClient
108
+
109
+ client = InferenceClient(model="mistralai/Mixtral-8x7B-Instruct-v0.1")
110
+
111
+ output = client.text_generation("Make this english to Japanese Hiragana. ex. Robert->はろー HuggingFace->はぎんぐふぇいす "+Vtubername+"->")
112
+ hiragana = ""
113
+ for char in output:
114
+ if '\u3040' <= char <= '\u309f':
115
+ hiragana += char
116
+
117
+
118
+
119
+ def get_brightness(color):
120
+ red, green, blue = color
121
+ return (red * 0.299 + green * 0.587 + blue * 0.114) / 255
122
+
123
+ brighter_color = secondary_color if get_brightness(dominant_colors[1]) > get_brightness(dominant_colors[2]) else third_color
124
+ darker_cplor = secondary_color if get_brightness(dominant_colors[1]) < get_brightness(dominant_colors[2]) else third_color
125
+
126
+ font_color=brighter_color
127
+
128
+ font_size=100
129
+ stroke_width=int(100*0.1)
130
+ stroke_color=darker_cplor
131
+ # Load the font
132
+ font = ImageFont.truetype("oshigo.otf", size=font_size)
133
+
134
+ japanese_text = hiragana
135
+
136
+ # Image setup
137
+ tile_width, tile_height = int(font_size*1.4), int(font_size*1.4) # Size of individual tiles
138
+ num_tiles = len(japanese_text)
139
+ total_width = tile_width * num_tiles
140
+ total_height = tile_height
141
+
142
+ # Create a new blank image
143
+ result_image = Image.new('RGBA', (total_width, total_height), (0, 0, 0, 0))
144
+ draw = ImageDraw.Draw(result_image)
145
+
146
+ for i, char in enumerate(japanese_text):
147
+ # Create an image for each character with transparency
148
+ tile_image = Image.new('RGBA', (tile_width, tile_height), (0, 0, 0, 0))
149
+ tile_draw = ImageDraw.Draw(tile_image)
150
+ # Calculate text position: random within the tile
151
+ text_width, text_height = draw.textsize(char, font=font)
152
+ x = random.randint(0, (tile_width - text_width)//1.25)
153
+ y = random.randint(0, (tile_height - text_height)//1.25)
154
+ # Draw text on the tile
155
+ tile_draw.text((x, y), char, font=font, fill="white", stroke_width=stroke_width, stroke_fill=stroke_color)
156
+
157
+ # Paste the tile into the result image
158
+ result_image.paste(tile_image, (i * tile_width, 0), tile_image)
159
+
160
+ # Save or display the image
161
+ caption = result_image
162
+
163
+ def resize_caption_to_logo(logo, caption):
164
+ logo_width = logo.width
165
+ target_caption_width = int(0.6 * logo_width)
166
+
167
+ if caption.width > target_caption_width:
168
+ aspect_ratio = caption.height / caption.width
169
+ new_height = int(target_caption_width * aspect_ratio)
170
+
171
+ resized_caption = caption.resize((target_caption_width, new_height), Image.ANTIALIAS)
172
+ return resized_caption
173
+ else:
174
+ return caption
175
+ caption = resize_caption_to_logo(logo, caption)
176
+
177
+
178
+ center=((title_logo.width - caption.width) // 2,title_logo.height//2)
179
+ bottom=(title_logo.width-caption.width)//2,int(title_logo.height-caption.height-40)
180
+ lower_right=(title_logo.width-caption.width,int(title_logo.height-caption.height-40))
181
+ upper_right=(title_logo.width-caption.width,int(caption.height+40))
182
+
183
+
184
+ # Define the possible positions
185
+ positions = [
186
+ ("center", center),
187
+ ("bottom", bottom),
188
+ ("lower_right", lower_right),
189
+ ("upper_right", upper_right),
190
+ ]
191
+
192
+ # Randomly select a position
193
+ position, coordinates = random.choice(positions)
194
+
195
+ # Paste the caption at the selected position
196
+ title_logo.paste(caption, coordinates, caption)
197
+ return title_logo
198
+
199
+ css="""
200
+ .gradio-container{
201
+ background-color: #fff;
202
+ background-image:
203
+ radial-gradient(#b4f3ea 0%, transparent 30%),
204
+ radial-gradient(#ffffcc 0%, transparent 30%); background-size: 40px 40px;
205
+ background-position: 0 0, 20px 20px;
206
+ }
207
+ h1{
208
+ font-size: 400%!important;
209
+ background: linear-gradient(to bottom, pink, white);
210
+ -webkit-background-clip: text;
211
+ -webkit-text-fill-color: transparent;
212
+ -webkit-text-stroke: 2px pink;
213
+ -webkit-text-stroke-width: 2px;
214
+ -webkit-text-stroke-color: pink;
215
+ }
216
+ """