import edimg import numpy as np from PIL import Image, ImageDraw, ImageFont, ImageColor, ImageFilter, ImageSequence import math import shutil import fn import fns def find_smallest_n(x, y): # 97n > x 조건을 만족하는 최소 n 계산 n_for_x = math.ceil(x / 97) # 150n > y 조건을 만족하는 최소 n 계산 n_for_y = math.ceil(y / 150) # 두 조건을 모두 만족하는 최소 n n = max(n_for_x, n_for_y) return n remove_image_path = 'resource/remove_black.png' # Adjust the path according to your setup remove_img_black = Image.open(remove_image_path).convert("RGBA") def attach_gif(gif, overlay): # 결과 프레임들을 저장할 리스트 frames = [] durations = [] x, y = gif.size n = find_smallest_n(x, y) target_width = 97 * n target_height = 150 * n overlay = overlay.resize((target_width,target_height), Image.Resampling.LANCZOS) remove_img = remove_img_black.resize((target_width, target_height), Image.Resampling.BOX) # GIF의 각 프레임을 순회하며 처리합니다. for frame in ImageSequence.Iterator(gif): durations.append(frame.info['duration']) # PNG 이미지를 GIF 프레임과 같은 크기로 조정합니다. # 이 부분은 필요에 따라 조정할 수 있습니다 resized_frame = frame.copy() img = resized_frame # 원본 이미지의 크기와 비율 계산 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) img.paste(overlay, (0, 0), overlay) img.paste(remove_img, (0, 0), remove_img) frame = img.quantize(method=2) # 두 이미지를 합칩니다. #combined = Image.alpha_composite(frame.convert('RGBA'), overlay) # 결과 리스트에 추가합니다. frames.append(img) # 모든 프레임을 처리한 후, 새로운 GIF로 저장합니다. frames[1].save('output.gif', save_all=True, append_images=frames[1:], loop=0, duration=durations, disposal=0, optimize=True) krtime = fn.get_kr_time() shutil.copy2("output.gif", f"/data/cache/{krtime}.gif")