fix later too tired
This commit is contained in:
parent
8021d17d27
commit
d300a3c812
8 changed files with 170 additions and 457 deletions
112
modules/image.py
112
modules/image.py
|
@ -38,76 +38,74 @@ async def gen_meme(input_image_path, sentence_size=5, max_attempts=10, custom_te
|
|||
if not markov_model or not os.path.isfile(input_image_path):
|
||||
return None
|
||||
|
||||
def generate_text():
|
||||
if custom_text:
|
||||
return custom_text
|
||||
|
||||
if sentence_size == 1:
|
||||
candidate = markov_model.make_short_sentence(max_chars=100, tries=100)
|
||||
if candidate:
|
||||
candidate = candidate.split()[0]
|
||||
return candidate
|
||||
else:
|
||||
candidate = markov_model.make_sentence(tries=100, max_words=sentence_size)
|
||||
if candidate:
|
||||
return improve_sentence_coherence(candidate)
|
||||
print(candidate)
|
||||
return None
|
||||
|
||||
|
||||
def draw_centered_text(img, text):
|
||||
draw = ImageDraw.Draw(img)
|
||||
width, height = img.size
|
||||
font_size = int(height / 10)
|
||||
font = load_font(font_size)
|
||||
|
||||
cleaned = re.sub(r'[^\w\s]', '', text).lower()
|
||||
coherent = rephrase_for_coherence(cleaned).upper()
|
||||
|
||||
bbox = draw.textbbox((0, 0), coherent, font=font)
|
||||
text_width, text_height_px = bbox[2] - bbox[0], bbox[3] - bbox[1]
|
||||
max_text_height = height // 4
|
||||
|
||||
if text_width <= width and text_height_px <= max_text_height:
|
||||
draw_text_with_outline(draw, coherent, (width - text_width) / 2, 0, font)
|
||||
img.save(input_image_path)
|
||||
return True
|
||||
|
||||
top_text, bottom_text = split_text_to_fit(coherent, font, width, draw)
|
||||
top_bbox = draw.textbbox((0, 0), top_text, font=font)
|
||||
bottom_bbox = draw.textbbox((0, 0), bottom_text, font=font)
|
||||
|
||||
top_height = top_bbox[3] - top_bbox[1]
|
||||
bottom_height = bottom_bbox[3] - bottom_bbox[1]
|
||||
|
||||
if top_height <= max_text_height and bottom_height <= max_text_height:
|
||||
draw_text_with_outline(draw, top_text, (width - (top_bbox[2] - top_bbox[0])) / 2, 0, font)
|
||||
y_bottom = height - bottom_height - int(height * 0.04)
|
||||
draw_text_with_outline(draw, bottom_text, (width - (bottom_bbox[2] - bottom_bbox[0])) / 2, y_bottom, font)
|
||||
img.save(input_image_path)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
attempt = 0
|
||||
while attempt < max_attempts:
|
||||
response = generate_text() or "NO TEXT GENERATED"
|
||||
with Image.open(input_image_path).convert("RGBA") as img:
|
||||
draw = ImageDraw.Draw(img)
|
||||
width, height = img.size
|
||||
|
||||
font_size = int(height / 10)
|
||||
font = load_font(font_size)
|
||||
|
||||
response = None
|
||||
if custom_text:
|
||||
response = custom_text
|
||||
else:
|
||||
for _ in range(20):
|
||||
if sentence_size == 1:
|
||||
candidate = markov_model.make_short_sentence(max_chars=100, tries=100)
|
||||
if candidate:
|
||||
candidate = candidate.split()[0]
|
||||
else:
|
||||
candidate = markov_model.make_sentence(tries=100, max_words=sentence_size)
|
||||
|
||||
if candidate and candidate not in generated_sentences:
|
||||
if sentence_size > 1:
|
||||
candidate = improve_sentence_coherence(candidate)
|
||||
generated_sentences.add(candidate)
|
||||
response = candidate
|
||||
break
|
||||
|
||||
if not response:
|
||||
response = "NO TEXT GENERATED"
|
||||
|
||||
cleaned_response = re.sub(r'[^\w\s]', '', response).lower()
|
||||
coherent_response = rephrase_for_coherence(cleaned_response).upper()
|
||||
|
||||
bbox = draw.textbbox((0, 0), coherent_response, font=font)
|
||||
text_width = bbox[2] - bbox[0]
|
||||
text_height_px = bbox[3] - bbox[1]
|
||||
max_text_height = height // 4
|
||||
|
||||
if text_width <= width and text_height_px <= max_text_height:
|
||||
draw_text_with_outline(draw, coherent_response, (width - text_width) / 2, 0, font)
|
||||
img.save(input_image_path)
|
||||
if draw_centered_text(img, response):
|
||||
return input_image_path
|
||||
else:
|
||||
top_text, bottom_text = split_text_to_fit(coherent_response, font, width, draw)
|
||||
|
||||
top_bbox = draw.textbbox((0, 0), top_text, font=font)
|
||||
bottom_bbox = draw.textbbox((0, 0), bottom_text, font=font)
|
||||
|
||||
top_height = top_bbox[3] - top_bbox[1]
|
||||
bottom_height = bottom_bbox[3] - bottom_bbox[1]
|
||||
|
||||
if top_height <= max_text_height and bottom_height <= max_text_height:
|
||||
draw_text_with_outline(draw, top_text, (width - (top_bbox[2] - top_bbox[0])) / 2, 0, font)
|
||||
y_bottom = height - bottom_height - int(height * 0.04)
|
||||
draw_text_with_outline(draw, bottom_text, (width - (bottom_bbox[2] - bottom_bbox[0])) / 2, y_bottom, font)
|
||||
img.save(input_image_path)
|
||||
return input_image_path
|
||||
|
||||
attempt += 1
|
||||
|
||||
with Image.open(input_image_path).convert("RGBA") as img:
|
||||
draw = ImageDraw.Draw(img)
|
||||
width, height = img.size
|
||||
font_size = int(height / 10)
|
||||
font = load_font(font_size)
|
||||
|
||||
truncated = coherent_response[:100]
|
||||
truncated = (rephrase_for_coherence(re.sub(r'[^\w\s]', '', "NO TEXT GENERATED").lower()).upper())[:100]
|
||||
bbox = draw.textbbox((0, 0), truncated, font=font)
|
||||
text_width = bbox[2] - bbox[0]
|
||||
|
||||
draw_text_with_outline(draw, truncated, (width - text_width) / 2, 0, font)
|
||||
img.save(input_image_path)
|
||||
return input_image_path
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue