add gen_image.py
This commit is contained in:
350
instagen/utils/gen_image.py
Normal file
350
instagen/utils/gen_image.py
Normal file
@@ -0,0 +1,350 @@
|
||||
import os
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFilter, ImageFont
|
||||
|
||||
# image_directory = 'input/images/logos-bw/{filename}.{ext}'
|
||||
|
||||
# font_regular_path = "input/fonts/DINAlternate-Bold.ttf"
|
||||
# font_condensed_path = "input/fonts/DINCondensed-Bold.ttf"
|
||||
font_regular_path = "instagen/static/instagen/fonts/scala/ScalaSans-BoldLF.otf"
|
||||
font_condensed_path = "instagen/static/instagen/fonts/scala/ScalaSans-BoldLF.otf"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Team:
|
||||
name: str
|
||||
winlosstie: list[int] = None
|
||||
image_directory: str = "../input/images/logos-bw/{filename}.{ext}"
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self.name.lower().replace(" ", "-")
|
||||
|
||||
@property
|
||||
def image(self):
|
||||
path = self.image_directory.format(filename=self.id, ext="png")
|
||||
if os.path.isfile(path):
|
||||
return path
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
@dataclass
|
||||
class Location:
|
||||
name: str
|
||||
address1: str = ""
|
||||
address2: str = ""
|
||||
image_directory: str = "instagen/static/instagen/ig/locations/{filename}.{ext}"
|
||||
|
||||
@property
|
||||
def id(self):
|
||||
return self.name.lower().replace(" ", "-")
|
||||
|
||||
@property
|
||||
def image(self):
|
||||
path = self.image_directory.format(filename=self.id, ext="png")
|
||||
if os.path.isfile(path):
|
||||
return path
|
||||
else:
|
||||
return None
|
||||
|
||||
@property
|
||||
def address(self):
|
||||
return ",".join([self.address1, self.address2])
|
||||
|
||||
|
||||
args = {
|
||||
"team_fave": Team("Hounds"),
|
||||
"team_opponent": Team("Trojans"),
|
||||
"home": False,
|
||||
"date": "2021-05-08 12:30 pm",
|
||||
"location": Location(
|
||||
"Maywood",
|
||||
image_directory="instagen/static/instagen/ig/locations/maywood.{ext}",
|
||||
),
|
||||
"runs_for": 8,
|
||||
"runs_against": 9,
|
||||
}
|
||||
|
||||
|
||||
def gen_image(
|
||||
team_fave,
|
||||
team_opponent,
|
||||
date,
|
||||
location=None,
|
||||
location_name=None,
|
||||
home=False,
|
||||
background="location",
|
||||
address=None,
|
||||
width=1080,
|
||||
height=1080,
|
||||
*kwargs,
|
||||
**args,
|
||||
):
|
||||
if not isinstance(date, datetime):
|
||||
# date = parser.parse(date)
|
||||
# date = date.astimezone(ZoneInfo("America/Chicago"))
|
||||
pass
|
||||
|
||||
if location.image and background == "location":
|
||||
background_image = Image.open(location.image).copy()
|
||||
background_image = background_image.resize((width, height))
|
||||
# background_image = background_image.filter(ImageFilter.GaussianBlur(radius=5))
|
||||
background_image = background_image.convert("RGBA")
|
||||
elif background == "transparent":
|
||||
background_image = Image.new("RGBA", (width, height), (0, 0, 0, 0))
|
||||
else:
|
||||
background_image = Image.new("RGBA", (width, height), (50, 55, 102))
|
||||
|
||||
title_images = []
|
||||
for team in [team_fave, team_opponent]:
|
||||
if team.image:
|
||||
title_images.append(Image.open(team.image).copy())
|
||||
else:
|
||||
title_images.append(Image.new("RGBA", (1080, 1080)))
|
||||
|
||||
title_image_left = title_images[0]
|
||||
title_image_right = title_images[1]
|
||||
|
||||
# Make a blank image for the rectangle, initialized to a completely
|
||||
# transparent color.
|
||||
tmp = Image.new("RGBA", background_image.size, (0, 0, 0, 0))
|
||||
|
||||
# Create a drawing context for it.
|
||||
draw = ImageDraw.Draw(tmp)
|
||||
|
||||
# section margin describes the margin of the section rectangles from the sides of the image
|
||||
section_margin_pct = 0.05
|
||||
llx = int(section_margin_pct * background_image.size[0])
|
||||
urx = int((1 - section_margin_pct) * background_image.size[0])
|
||||
lly = int((1 - section_margin_pct) * background_image.size[1])
|
||||
ury = int(0.50 * background_image.size[1])
|
||||
|
||||
lly2 = int(0.49 * background_image.size[1])
|
||||
ury2 = int(0.05 * background_image.size[1])
|
||||
|
||||
section_info = Image.open(
|
||||
Path(
|
||||
"instagen/static/instagen/graphics/{name}{ext}".format(
|
||||
name="sign-tan", ext=".png"
|
||||
)
|
||||
)
|
||||
)
|
||||
section_info_draw = ImageDraw.Draw(section_info)
|
||||
|
||||
section_title = Image.open(
|
||||
Path(
|
||||
"instagen/static/instagen/graphics/{name}{ext}".format(
|
||||
name="sign-green", ext=".png"
|
||||
)
|
||||
)
|
||||
)
|
||||
section_title_draw = ImageDraw.Draw(section_title)
|
||||
|
||||
# First line: Date
|
||||
font = ImageFont.truetype(font_regular_path, 62)
|
||||
text = f"{date:%a, %B %-d %-I:%M %p}".upper()
|
||||
# text = date
|
||||
text_size = draw.textsize(text, font)
|
||||
loc = (1050, 280)
|
||||
section_info_draw.text(loc, text, (14, 42, 28), font=font, anchor="ra")
|
||||
|
||||
# Second line: Venue
|
||||
font = ImageFont.truetype(font_condensed_path, 34)
|
||||
if not location_name:
|
||||
text = location.name.upper()
|
||||
else:
|
||||
text = location_name.upper()
|
||||
text_size = section_info_draw.textsize(text, font)
|
||||
loc = (1050, 355)
|
||||
section_info_draw.text(loc, text, (14, 42, 28), font=font, anchor="ra")
|
||||
|
||||
font = ImageFont.truetype(font_regular_path, 80)
|
||||
if home:
|
||||
text = "VS"
|
||||
else:
|
||||
text = "AT"
|
||||
text_size = section_title_draw.textsize(text, font)
|
||||
loc = (540, 120)
|
||||
color = (255, 255, 255)
|
||||
section_title_draw.text(loc, text, color, font=font, anchor="mm")
|
||||
|
||||
# Alpha composite the two images together.
|
||||
background_image = Image.alpha_composite(background_image, tmp)
|
||||
|
||||
# Title Image Left
|
||||
title_image_left.thumbnail([350, 350])
|
||||
loc = (50, -50)
|
||||
section_title.paste(title_image_left, loc, title_image_left)
|
||||
|
||||
# Title Image Right
|
||||
title_image_right.thumbnail([350, 350])
|
||||
loc = (650, -50)
|
||||
section_title.paste(title_image_right, loc, title_image_right)
|
||||
|
||||
# background_image.paste(section_info, (llx, ury), section_info)
|
||||
# background_image.paste(section_title, (llx, ury2), section_title)
|
||||
section_title.paste(section_info, (0, 0), section_info)
|
||||
section_title.thumbnail([800, 800])
|
||||
|
||||
if background == "badge":
|
||||
return section_title
|
||||
|
||||
background_image.paste(
|
||||
section_title,
|
||||
(int((background_image.size[0] - section_title.size[0]) / 2), height - 360),
|
||||
section_title,
|
||||
)
|
||||
|
||||
return background_image
|
||||
|
||||
|
||||
def gen_results_image(
|
||||
team_fave,
|
||||
team_opponent,
|
||||
date,
|
||||
location=None,
|
||||
location_name=None,
|
||||
home=False,
|
||||
background="location",
|
||||
address=None,
|
||||
width=1080,
|
||||
height=1080,
|
||||
runs_for=0,
|
||||
runs_against=0,
|
||||
*kwargs,
|
||||
**args,
|
||||
):
|
||||
if not isinstance(date, datetime):
|
||||
# date = parser.parse(date)
|
||||
# date = date.astimezone(ZoneInfo("America/Chicago"))
|
||||
pass
|
||||
|
||||
if location.image and background == "location":
|
||||
background_image = Image.open(location.image).copy()
|
||||
background_image = background_image.resize((width, height))
|
||||
# background_image = background_image.filter(ImageFilter.GaussianBlur(radius=5))
|
||||
background_image = background_image.convert("RGBA")
|
||||
elif background == "transparent":
|
||||
background_image = Image.new("RGBA", (width, height), (0, 0, 0, 0))
|
||||
else:
|
||||
background_image = Image.new("RGBA", (width, height), (50, 55, 102))
|
||||
|
||||
title_images = []
|
||||
for team in [team_fave, team_opponent]:
|
||||
if team.image:
|
||||
title_images.append(Image.open(team.image).copy())
|
||||
else:
|
||||
title_images.append(Image.new("RGBA", (1080, 1080)))
|
||||
|
||||
title_image_left = title_images[0]
|
||||
title_image_right = title_images[1]
|
||||
|
||||
# Make a blank image for the rectangle, initialized to a completely
|
||||
# transparent color.
|
||||
tmp = Image.new("RGBA", background_image.size, (0, 0, 0, 0))
|
||||
|
||||
# Create a drawing context for it.
|
||||
draw = ImageDraw.Draw(tmp)
|
||||
|
||||
# section margin describes the margin of the section rectangles from the sides of the image
|
||||
section_margin_pct = 0.05
|
||||
llx = int(section_margin_pct * background_image.size[0])
|
||||
urx = int((1 - section_margin_pct) * background_image.size[0])
|
||||
lly = int((1 - section_margin_pct) * background_image.size[1])
|
||||
ury = int(0.50 * background_image.size[1])
|
||||
|
||||
lly2 = int(0.49 * background_image.size[1])
|
||||
ury2 = int(0.05 * background_image.size[1])
|
||||
|
||||
section_info = Image.open(
|
||||
Path(
|
||||
"instagen/static/instagen/graphics/{name}{ext}".format(
|
||||
name="sign-tan", ext=".png"
|
||||
)
|
||||
)
|
||||
)
|
||||
section_info_draw = ImageDraw.Draw(section_info)
|
||||
|
||||
section_title = Image.open(
|
||||
Path(
|
||||
"instagen/static/instagen/graphics/{name}{ext}".format(
|
||||
name="sign-green", ext=".png"
|
||||
)
|
||||
)
|
||||
)
|
||||
section_title_draw = ImageDraw.Draw(section_title)
|
||||
|
||||
# First line: Results
|
||||
loc = (1050, 265)
|
||||
if runs_for > runs_against:
|
||||
result_letter = "W"
|
||||
elif runs_for < runs_against:
|
||||
result_letter = "L"
|
||||
elif runs_for == runs_against:
|
||||
result_letter = "T"
|
||||
font = ImageFont.truetype(font_regular_path, 100)
|
||||
section_info_draw.text(
|
||||
loc,
|
||||
f"FINAL: {result_letter} {runs_for}-{runs_against}",
|
||||
(14, 42, 28),
|
||||
font=font,
|
||||
anchor="ra",
|
||||
)
|
||||
|
||||
# Second line: Date
|
||||
text = f"{date:%a, %B %-d %-I:%M %p}".upper()
|
||||
# text = date
|
||||
font = ImageFont.truetype(font_condensed_path, 34)
|
||||
text_size = section_info_draw.textsize(text, font)
|
||||
loc = (1050, 355)
|
||||
section_info_draw.text(loc, text, (14, 42, 28), font=font, anchor="ra")
|
||||
|
||||
font = ImageFont.truetype(font_regular_path, 80)
|
||||
if home:
|
||||
text = "VS"
|
||||
else:
|
||||
text = "AT"
|
||||
text_size = section_title_draw.textsize(text, font)
|
||||
loc = (540, 120)
|
||||
color = (255, 255, 255)
|
||||
section_title_draw.text(loc, text, color, font=font, anchor="mm")
|
||||
|
||||
# Alpha composite the two images together.
|
||||
background_image = Image.alpha_composite(background_image, tmp)
|
||||
|
||||
# Title Image Left
|
||||
title_image_left.thumbnail([350, 350])
|
||||
loc = (50, -50)
|
||||
section_title.paste(title_image_left, loc, title_image_left)
|
||||
|
||||
# Title Image Right
|
||||
title_image_right.thumbnail([350, 350])
|
||||
loc = (650, -50)
|
||||
section_title.paste(title_image_right, loc, title_image_right)
|
||||
|
||||
# background_image.paste(section_info, (llx, ury), section_info)
|
||||
# background_image.paste(section_title, (llx, ury2), section_title)
|
||||
section_title.paste(section_info, (0, 0), section_info)
|
||||
section_title.thumbnail([800, 800])
|
||||
|
||||
if background == "badge":
|
||||
return section_title
|
||||
|
||||
background_image.paste(
|
||||
section_title,
|
||||
(int((background_image.size[0] - section_title.size[0]) / 2), height - 360),
|
||||
section_title,
|
||||
)
|
||||
|
||||
# background_image.show()
|
||||
|
||||
return background_image
|
||||
|
||||
|
||||
# gen_results_image(**args)
|
||||
Reference in New Issue
Block a user