Skip to content

Commit

Permalink
Merge pull request #609 from jacebrowning/position-overlay
Browse files Browse the repository at this point in the history
Support custom overlay position
  • Loading branch information
jacebrowning committed Apr 1, 2021
2 parents b2c5f06 + b96b4cf commit 59b9587
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
15 changes: 13 additions & 2 deletions app/models.py
Expand Up @@ -60,18 +60,29 @@ def stylize(self, text: str, **kwargs) -> str:
return text


@datafile
class Overlay:

x: float = 0.5
y: float = 0.5
scale: float = 0.25


@datafile("../templates/{self.id}/config.yml", defaults=True)
class Template:

id: str
name: str = ""
source: Optional[str] = None

text: list[Text] = field(
default_factory=lambda: [Text(), Text(anchor_x=0.0, anchor_y=0.8)]
)
styles: list[str] = field(default_factory=lambda: [])
example: list[str] = field(default_factory=lambda: ["your text", "goes here"])

styles: list[str] = field(default_factory=lambda: [])
overlay: list[Overlay] = field(default_factory=lambda: [Overlay()])

def __str__(self):
return str(self.directory)

Expand Down Expand Up @@ -267,7 +278,7 @@ async def check(self, style: str) -> bool:
return False

try:
await asyncio.to_thread(utils.images.embed, Path(path), self.image)
await asyncio.to_thread(utils.images.embed, self, Path(path), self.image)
except (OSError, SyntaxError) as e:
logger.error(e)
await path.unlink()
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 20 additions & 4 deletions app/utils/images.py
Expand Up @@ -62,16 +62,20 @@ def load(path: Path) -> Image:
return image


def embed(foreground_path: Path, background_path: Path) -> Image:
def embed(template: Template, foreground_path: Path, background_path: Path) -> Image:
overlay = template.overlay[0]
foreground = load(foreground_path)
background = load(background_path)
size = (background.size[0] // 4, background.size[1] // 4)

size = background.size[0] * overlay.scale, background.size[1] * overlay.scale
foreground.thumbnail(size)

offset = (
(background.size[0] - foreground.size[0]) // 2,
(background.size[1] - foreground.size[1]) // 2,
int(background.size[0] * overlay.x - foreground.size[0] / 2),
int(background.size[1] * overlay.y - foreground.size[1] / 2),
)
background.paste(foreground, offset, foreground.convert("RGBA"))

background.convert("RGB").save(foreground_path)
return foreground_path

Expand Down Expand Up @@ -130,6 +134,18 @@ def render_image(
box = box.rotate(angle, resample=Image.BICUBIC, expand=True)
image.paste(box, point, box)

if settings.DEBUG:
draw = ImageDraw.Draw(image)
for overlay in template.overlay:
size = image.size[0] * overlay.scale, image.size[1] * overlay.scale
xy = (
image.size[0] * overlay.x - size[0] // 2,
image.size[1] * overlay.y - size[1] // 2,
image.size[0] * overlay.x + size[0] // 2,
image.size[1] * overlay.y + size[1] // 2,
)
draw.rectangle(xy, outline="lime")

if pad:
image = add_blurred_background(image, background, *size)

Expand Down
4 changes: 4 additions & 0 deletions templates/fine/config.yml
Expand Up @@ -17,6 +17,10 @@ text:
scale_y: 0.2
styles:
-
overlay:
- x: 0.425
y: 0.54
scale: 0.5
example:
- ''
- this is fine
4 changes: 4 additions & 0 deletions templates/iw/config.yml
Expand Up @@ -17,6 +17,10 @@ text:
scale_y: 0.2
styles:
-
overlay:
- x: 0.505
y: 0.515
scale: 0.525
example:
- does testing
- in production

0 comments on commit 59b9587

Please sign in to comment.