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)