objektify2 / edimg.py
tripleS-Dev
update V1.3.7L Beta
997dacc
raw
history blame
No virus
7.98 kB
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)