Spaces:
Sleeping
Sleeping
| import os | |
| from PIL import Image, ImageDraw, ImageFont, ImageOps | |
| from utils import Utils | |
| class TextProcessor: | |
| def apply_frontmatter(text, replacements): | |
| for key, value in replacements.items(): | |
| text = text.replace(f'{{{key}}}', value) | |
| return text | |
| class ImageProcessor: | |
| def __init__(self, size): | |
| _, self.temp_dir = Utils.get_tempdir() | |
| self.size = size | |
| def create_layer(self, layout='horizontal'): | |
| ts, temp_dir = Utils.get_tempdir() | |
| size = (int(self.size[0]), int(self.size[1])) # Convert to integers | |
| img = Image.new('RGBA', size, (255, 255, 255, 0)) | |
| draw = ImageDraw.Draw(img) | |
| draw.rectangle([(0, 0), (self.size[0], self.size[1])], outline="lightgray") | |
| return img | |
| def combine_images(self, base_image, overlay_path, size, position): | |
| try: | |
| base_img = base_image.convert("RGBA") | |
| with open(overlay_path, 'rb') as f: | |
| overlay_img = Image.open(f).convert("RGBA") | |
| overlay_img_resized = overlay_img.resize(size) | |
| if position is None: | |
| position = (0, 0) | |
| # Create a blank image with the same size as base_img | |
| temp_img = Image.new('RGBA', base_img.size, (255, 255, 255, 0)) | |
| temp_img.paste(overlay_img_resized, position, overlay_img_resized) | |
| # Alpha composite the temporary image with the base image | |
| base_img = Image.alpha_composite(base_img, temp_img) | |
| return base_img | |
| except Exception as e: | |
| raise Exception(f"Error combining images: {e}") | |
| # def combine_txt(self, base_image, text, size, position, font_path='fonts/msmincho.ttc', font_size=12, fill="black"): | |
| # try: | |
| # font_path = str(Path(__file__).parent / "../fonts/msmincho.ttc") | |
| # font = ImageFont.truetype(font_path, font_size) | |
| # base_img = base_image.convert("RGBA") | |
| # draw = ImageDraw.Draw(base_img) | |
| # w, h = size | |
| # draw.text(position, text, font=font, fill=fill) | |
| # return base_img | |
| # except Exception as e: | |
| # raise Exception(f"Error combining text: {e}") |