Spaces:
Running
Running
File size: 8,515 Bytes
00b57c1 ec0d219 c9470de a4f6704 64af843 a4f6704 c9470de 00b57c1 22e7fa9 00b57c1 22e7fa9 00b57c1 7d1e834 83fe570 00b57c1 6940fd9 00b57c1 ec0d219 00b57c1 3a8a569 00b57c1 0712f50 0bac937 0712f50 3a8a569 00b57c1 0712f50 00b57c1 f9c7696 8ba5375 a06749c 00b57c1 5a1adc5 7d1e834 5a1adc5 bb257c9 5a1adc5 bb257c9 9ef6d47 5a1adc5 |
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 |
from PIL import Image
from rembg import remove
import cairosvg
import io
import numpy as np
from sklearn.cluster import KMeans
from PIL import Image, ImageDraw, ImageFont
import random
import gradio as gr
def generate(logo=None, Vtubername="", sdkey=""):
if(logo==None):
gr.Warning('Please Select Your Photo📸')
if(Vtubername==""):
Vtubername = "unkown"
gr.Warning('Please Select Your Name😱')
if(sdkey==""):
gr.Warning('Please Set valid Stability AI API Key🔑')
def extract_dominant_colors(img, num_colors=3, ignore_edges=True):
if img.mode == 'RGBA':
image = img.convert('RGB')
else:
image = img
image = image.resize((150, 150))
data = np.array(image)
pixels = data.reshape(-1, 3)
if ignore_edges:
edge_pixels = np.concatenate([data[0, :, :], data[-1, :, :], data[:, 0, :], data[:, -1, :]], axis=0)
edge_colors, counts = np.unique(edge_pixels, axis=0, return_counts=True)
background_color = edge_colors[counts.argmax()]
pixels = pixels[~np.all(pixels == background_color, axis=1)]
if len(pixels) == 0:
return np.array([background_color,np.array([60,60,60]),np.array([255,255,255])])
elif len(pixels) == 1:
return np.array([pixels[0],np.array([60,60,60]),np.array([255,255,255])])
elif len(pixels) == 2:
return np.array([pixels[0],pixels[1],np.array([60,60,60])])
model = KMeans(n_clusters=3)
model.fit(pixels)
colors = model.cluster_centers_
colors = colors.round(0).astype(int)
return colors
dominant_colors = extract_dominant_colors(logo, num_colors=3)
template_prime_colors = {
"black color": [0, 0, 0],
"white": [255, 255, 255],
"red": [255, 0, 0],
"lightgreen": [0, 255, 0],
"blue": [0, 0, 255],
"yellow": [255, 255, 0],
"lightblue": [0, 255, 255],
"pink": [255, 0, 255],
"gray": [128, 128, 128],
"maroon": [128, 0, 0],
"olive": [128, 128, 0],
"green": [0, 128, 0],
"purple": [128, 0, 128],
"navy": [0, 0, 128],
"orange": [255, 165, 0],
"bluegreen": [0, 128, 128],
"lightpurple": [128, 128, 255],
"skyblue color": [0, 128, 255],
"brown": [139,69,19],
}
_primary_color = dominant_colors[0]
closest_color = "black color"
for color in template_prime_colors:
if np.linalg.norm(np.array(template_prime_colors[color]) - _primary_color) < np.linalg.norm(np.array(template_prime_colors[closest_color]) - _primary_color):
closest_color = color
primary_color = closest_color
print(primary_color)
secondary_color=str("rgb("+str(dominant_colors[1][0])+", "+str(dominant_colors[1][1])+", "+str(dominant_colors[1][2])+")")
third_color=str("rgb("+str(dominant_colors[2][0])+", "+str(dominant_colors[2][1])+", "+str(dominant_colors[2][2])+")")
import requests
from huggingface_hub import InferenceClient
client = InferenceClient(model="mistralai/Mixtral-8x7B-Instruct-v0.1")
output = client.text_generation("Make this english to Japanese Hiragana. ex. Robert->はろー HuggingFace->はぎんぐふぇいす "+Vtubername+"->")
hiragana = ""
for char in output:
if '\u3040' <= char <= '\u309f':
hiragana += char
response = requests.post(
f"https://api.stability.ai/v2beta/stable-image/generate/sd3",
headers={
"authorization": f"Bearer "+sdkey,
"accept": "image/*"
},
files={"none": ''},
data={
"model": "sd3",
"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.",
"negative_prompt": "subtitle,face, ruby text, smoke, 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",
"output_format": "jpeg",
},
)
image = None
if response.status_code == 200:
image = response.content
else:
gr.Warning('Your message is not allowed!')
raise Exception(str(response.json()))
image = Image.open(io.BytesIO(response.content))
title_logo=remove(image)
def get_brightness(color):
red, green, blue = color
return (red * 0.299 + green * 0.587 + blue * 0.114) / 255
brighter_color = secondary_color if get_brightness(dominant_colors[1]) > get_brightness(dominant_colors[2]) else third_color
darker_cplor = secondary_color if get_brightness(dominant_colors[1]) < get_brightness(dominant_colors[2]) else third_color
font_color=brighter_color
font_size=100
stroke_width=int(100*0.1)
stroke_color=darker_cplor
# Load the font
font = ImageFont.truetype("oshigo.otf", size=font_size)
japanese_text = hiragana
# Image setup
tile_width, tile_height = int(font_size*1.4), int(font_size*1.4) # Size of individual tiles
num_tiles = len(japanese_text)
total_width = tile_width * num_tiles
total_height = tile_height
# Create a new blank image
result_image = Image.new('RGBA', (total_width, total_height), (0, 0, 0, 0))
draw = ImageDraw.Draw(result_image)
for i, char in enumerate(japanese_text):
# Create an image for each character with transparency
tile_image = Image.new('RGBA', (tile_width, tile_height), (0, 0, 0, 0))
tile_draw = ImageDraw.Draw(tile_image)
# Calculate text position: random within the tile
text_width, text_height = draw.textsize(char, font=font)
x = random.randint(0, (tile_width - text_width)//1.25)
y = random.randint(0, (tile_height - text_height)//1.25)
# Draw text on the tile
tile_draw.text((x, y), char, font=font, fill="white", stroke_width=stroke_width, stroke_fill=stroke_color)
# Paste the tile into the result image
result_image.paste(tile_image, (i * tile_width, 0), tile_image)
# Save or display the image
caption = result_image
def resize_caption_to_logo(logo, caption):
if caption.width > logo.width:
scaler = 2.4
resized_caption = caption.resize((int(logo.width*scaler), int(scaler*caption.height * logo.width / caption.width )))
print("resizing")
return resized_caption
else:
return caption
caption = resize_caption_to_logo(logo, caption)
center=((title_logo.width - caption.width) // 2,title_logo.height//2)
bottom=(title_logo.width-caption.width)//2,int(title_logo.height-caption.height-100)
lower_right=(title_logo.width-caption.width-40,int(title_logo.height-caption.height-80))
upper_right=(title_logo.width-caption.width-40,int(caption.height+80))
# Define the possible positions
positions = [
("center", center),
("bottom", bottom),
("lower_right", lower_right),
("upper_right", upper_right),
]
# Randomly select a position
position, coordinates = random.choice(positions)
# Paste the caption at the selected position
title_logo.paste(caption, coordinates, caption)
return title_logo
css="""
.gradio-container{
background-color: #fff;
background-image:
radial-gradient(#b4f3ea 0%, transparent 30%),
radial-gradient(#ffffcc 0%, transparent 30%); background-size: 40px 40px;
background-position: 0 0, 20px 20px;
}
h1{
font-size: 400%!important;
background: linear-gradient(to bottom, pink, white);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
-webkit-text-stroke: 2px pink;
-webkit-text-stroke-width: 2px;
-webkit-text-stroke-color: pink;
}
"""
import os
iface = gr.Interface(
theme=gr.themes.Default(primary_hue="pink",font=[gr.themes.GoogleFont("Mochiy Pop One")]),
css=css,
fn=generate,
inputs=[gr.Image(label="Your Photo", type="pil"), gr.Textbox(label="Your Name(*alphabet only!*)"), gr.Textbox(label="Stability AI API Key")],
outputs=gr.Image(label="Generated Logo"),
title="Kawaii Logo Generator",
description="①Upload photo you wanna make Kawaii❤️ <br>② Input the name(*alphabet only!*)⭐️ <br>③ Set your Stability AI API key🔑(https://platform.stability.ai/account/keys) <br>④Press Submit🧙",
#examples=[["image.jpeg", "gojiteji", os.environ["sdkey"]]],
allow_flagging=False
)
# Launch the interface
iface.launch(debug=True) |