Spaces:
Running
Running
File size: 7,980 Bytes
9793ed6 5c09508 9793ed6 5c09508 9793ed6 5c09508 9793ed6 5c09508 9793ed6 997dacc 9793ed6 997dacc 9793ed6 |
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 |
from PIL import Image, ImageDraw, ImageFont, ImageColor, ImageFilter, ImageSequence
import numpy as np
import qrcode
def paste_img(img, img2, coordinate):
if not isinstance(img, Image.Image):
img = Image.open(img).convert("RGBA")
if not isinstance(img2, Image.Image):
img2 = Image.open(img2).convert("RGBA")
img.paste(img2, coordinate, img2)
return img
def color(img, color):
if not isinstance(img, Image.Image):
img = Image.open(img).convert("RGBA")
image_np = np.array(img)
_, _, _, alpha = image_np.T
mask = alpha > 0
image_np[..., :-1][mask.T] = ImageColor.getcolor(color, "RGB")
img = Image.fromarray(image_np)
return img
def txt(text, r, g, b, font, angle, rl, fonts = "font/Helvetica_Neue_LT_Std_75_Bold.otf", image_size = (400, 100), opacity = 255):
# ์ด๋ฏธ์ง ํฌ๊ธฐ์ ๊ธ๊ผด ํฌ๊ธฐ ์ค์
font_size = font
# ์ด๋ฏธ์ง์ ๋๋ก์ ๊ฐ์ฒด ์์ฑ
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, opacity)
# ํ
์คํธ ๊ทธ๋ฆฌ๊ธฐ
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]
match rl:
case "r":
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)
case "l":
# ํ
์คํธ๋ฅผ ์ผ์ชฝ์ผ๋ก ์ ๋ ฌํ์ฌ ๊ทธ๋ฆฌ๊ธฐ
text_x = 0 # ์ผ์ชฝ ๋ง์ถค
text_y = (image_size[1] - text_height) / 2
draw.text((text_x, text_y), text, fill=text_color, font=font)
# ์ด๋ฏธ์ง ํ์
if angle != 0:
image = image.rotate(angle, expand=True)
return image
def serial(text, r, g, b, font_size):
# ์ด๋ฏธ์ง ํฌ๊ธฐ์ ๊ธ๊ผด ํฌ๊ธฐ ์ค์
text = "#"+ text
image_size = (400, 100) # ๋๋น x ๋์ด
fonts = "font/MatrixSSK Regular.ttf"
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
remove_image_path = f'resources/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)
def color_ai(num):
grimg = Image.open(f'grad2/gradient_{num}.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)
return grimg
def color_ai_back(num):
file_path = f'grad2/gradient_{num}.png'
img = Image.open(file_path)
# ์๋ผ๋ผ ๋ถ๋ถ ์ค์ (x < 126์ ํด๋นํ๋ ๋ถ๋ถ)
left = 125
top = 0
right = 1289 # x๊ฐ 126๋ณด๋ค ์์ ๋ถ๋ถ๊น์ง
bottom = 1800
# ์ด๋ฏธ์ง ์๋ฅด๊ธฐ
cropped_img = img.crop((left, top, right, bottom))
cropped_img = rounded(cropped_img)
return cropped_img
remove_image_path_rounded = 'resources/remove.png' # Adjust the path according to your setup
remove_img_rounded = Image.open(remove_image_path_rounded).convert("RGBA")
remove_np_rounded = np.array(remove_img_rounded)
# remove.png์์ r ์ฑ๋์ด 255์ด๊ณ ์ํ ์ฑ๋์ด ์๋ ํฝ์
์์น ์ฐพ๊ธฐ
r_channel_rounded = remove_np_rounded[:, :, 0]
alpha_channel_rounded = remove_np_rounded[:, :, 3]
mask_rounded = (r_channel_rounded == 255) & (alpha_channel_rounded > 0)
def rounded(img):
img = img.convert("RGBA")
img_np = np.array(img)
# ํด๋น ์์น์ resized_image ํฝ์
์ ํฌ๋ช
ํ๊ฒ ์ค์
img_np[mask_rounded, :] = (0, 0, 0, 0) # RGBA ํ์์ ๋ง๊ฒ ์กฐ์
img = Image.fromarray(img_np)
return img
def rounded_color(img, r, g, b):
img = img.convert("RGBA")
img_np = np.array(img)
# ํด๋น ์์น์ resized_image ํฝ์
์ ํฌ๋ช
ํ๊ฒ ์ค์
img_np[mask_rounded, :] = (r, g, b, 255) # RGBA ํ์์ ๋ง๊ฒ ์กฐ์
img = Image.fromarray(img_np)
return img
def resize_img(image, target_width, target_height):
if not isinstance(image, Image.Image):
img = Image.open(image).convert("RGBA")
else:
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 calculate_size(text, font_size, font_path):
# Create a dummy image just to create a draw object
dummy_image = Image.new('RGB', (1100, 136))
draw = ImageDraw.Draw(dummy_image)
# Load the font
font = ImageFont.truetype(font_path, font_size)
# Calculate text width using the defined draw object
text_width = draw.textlength(text, font=font)
return int(text_width)
def qr(url):
# Generate QR code with a smaller border
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=2, # 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')
qr_img = qr_img.resize((335,335), Image.Resampling.LANCZOS)
return qr_img
def qr_icon(icon_path):
img_icon = Image.open(icon_path)
img_icon = resize_img(img_icon, 84,84)
return img_icon
def makegif(gif, back):
# ๊ฒฐ๊ณผ ํ๋ ์๋ค์ ์ ์ฅํ ๋ฆฌ์คํธ
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(back, (x,0))
# ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ์ถ๊ฐํฉ๋๋ค.
frames.append(img)
# ๋ชจ๋ ํ๋ ์์ ์ฒ๋ฆฌํ ํ, ์๋ก์ด GIF๋ก ์ ์ฅํฉ๋๋ค.
frames[1].save(f'exgif.gif', save_all=True, append_images=frames[1:], loop=0, duration=durations, disposal=0, optimize=True) |