Spaces:
Sleeping
Sleeping
File size: 2,213 Bytes
7c7b7af b5e972d 6c64dd2 b5e972d 7c7b7af b5e972d 7c7b7af b5e972d 7c7b7af b5e972d 7c7b7af 6c64dd2 7c7b7af 6c64dd2 7c7b7af 6c64dd2 7c7b7af 470f9a1 |
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 |
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}") |