From bb42eec31f5f666a2e377a954b3b391083b4f595 Mon Sep 17 00:00:00 2001 From: yair-mantis Date: Sat, 20 Jan 2024 18:00:05 +0200 Subject: [PATCH] handle new lines --- nik2.py | 27 ++++++++++++ nikud.py | 122 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 93 insertions(+), 56 deletions(-) create mode 100644 nik2.py diff --git a/nik2.py b/nik2.py new file mode 100644 index 0000000..9228a85 --- /dev/null +++ b/nik2.py @@ -0,0 +1,27 @@ +import streamlit as st +from wand.image import Image as wImage +from wand.drawing import Drawing +from wand.color import Color + +draw = Drawing() +font = "fonts\\DejaVuSans.ttf" +draw.font = font +img = wImage(width=696, height=100, background=Color('#ffffff')) +# draw.fill_color(Color('#000000')) +draw.text_alignment = 'right' +draw.text_antialias = True +draw.text_encoding = 'utf-8' +# draw.text_interline_spacing = 1 +# draw.text_interword_spacing = 15.0 +# draw.text_kerning = 0.0 +draw.font_size = 40 + +draw.text( + int(img.width / 2), + int(img.height / 2), + u'יֵשׁ לְךָ חָבֵר חָדָשׁ' + ) + +draw(img) +img_bytes = img.make_blob(format='png') +st.image(img_bytes) diff --git a/nikud.py b/nikud.py index b419bf6..9657aa6 100644 --- a/nikud.py +++ b/nikud.py @@ -1,21 +1,22 @@ # -*- coding: utf-8 -*- +import os +from dotenv import load_dotenv import streamlit as st import requests -from PIL import Image, ImageDraw, ImageFont -from dotenv import load_dotenv -import os -from bidi.algorithm import get_display + +from wand.image import Image +from wand.drawing import Drawing +from wand.color import Color # Load environment variables from .env file load_dotenv() -# Read API key from .env file api_key = os.getenv("API_KEY") # Specify the font size -font_size = 80 -# font = ImageFont.truetype("fonts/BonaNova-Regular.ttf", font_size, encoding='unic') -font = ImageFont.truetype("fonts/DejaVuSans.ttf", font_size) # only one that works +font_size = 40 +# Create an image +img_width = 696 # place the api_key in the .env file @@ -53,63 +54,72 @@ def main(): st.title('Dicta Nakdan API Interface') # Input fields for user - hebrew_text = st.text_area("Enter Hebrew Text:", "טקסט טקסט טקסט") + hebrew_text = st.text_area("Enter Hebrew Text:", "רסק שמופי טקסט", ) # Button to send request - if st.button("Process Text"): + if st.button("Process Text",): response = get_nakdan_response(hebrew_text, api_key) if not isinstance(response, dict): response = {'data': []} + # st.json(response) - # Extract words - words = [ - option['w'].replace("|", "") - for item in response['data'] - if 'nakdan' in item - for option in item['nakdan'].get('options', []) - ] - st.text(" ".join(words)) # printwords after each other - - # Create an image - img_width = 696 - img_height = 400 # font_size * (len(words) // 10 + 2) # Adjust the multiplier as needed - img = Image.new('RGB', (img_width, img_height), color=(255, 255, 255)) - d = ImageDraw.Draw(img) - - # Position for the first word - x, y = 10, 0 - - max_width = img_width - 20 # Maximum width for each line - line_width = 0 # Current line width - - for word in words: - bidi_word = get_display(word) # Convert word to RTL format - word_width = font.getbbox(bidi_word)[2] - font.getbbox(bidi_word)[0] # Width of the word - - # Check if the word fits in the current line - if line_width + word_width <= img_width - 20: - # Render the word in the current line - d.text((img_width - (x + line_width + word_width), y), bidi_word, fill=(0, 0, 0), font=font) - line_width += word_width + 10 # Add word width and spacing - else: - # Move to the next line - y += font_size # Move to the next line - line_width = 0 # Reset line width - - # Check if the word fits in the new line - if word_width <= img_width - 20: - # Render the word in the new line - d.text((img_width - (x + line_width + word_width), y), bidi_word, fill=(0, 0, 0), font=font) - line_width += word_width + 10 # Add word width and spacing + # Extract words or newline from response + words = [] + for item in response['data']: + if 'nakdan' in item: + if item['nakdan']['word'] == "\n": # Check if the word is a newline + words.append("\n") else: - # Word is too long for a single line, truncate it - truncated_word = bidi_word[:max_width // font_size] + "..." - d.text((img_width - (x + line_width + font.getsize(truncated_word)[0]), y), truncated_word, fill=(0, 0, 0), font=font) - line_width += font.getsize(truncated_word)[0] + 10 # Add truncated word width and spacing + words.extend( + option['w'].replace("|", "") + for option in item['nakdan'].get('options', []) + ) + # st.text(" ".join(words)) # Print words, including newlines - # st.text(f"Word: '{bidi_word}', Position: (x={x + line_width - word_width}, y={y}), {len(bidi_word)} characters") + draw = Drawing() + fontw = "fonts\\DejaVuSans.ttf" + draw.font = fontw + draw.text_antialias = True + draw.text_encoding = 'utf-8' + draw.font_size = font_size - st.image(img) + imgw = Image(width=696, height=400, background=Color('#ffffff')) + + spacing = 10 + line_width = 0 + y = font_size + x = 0 + for word in words: + if word == "\n": + y += font_size + line_width = 0 + continue + + # Create a dummy image to get the text metrics + with Image(width=1, height=1) as img: + metrics = draw.get_font_metrics(img, word) + word_width = int(metrics.text_width) + word_height = int(metrics.text_height) + + line_width += word_width + spacing + # Position for the first word + # Check if the word fits in the current line + if line_width <= img_width - 20: + x = img_width - (line_width) - 20 + # Render the word in the current line + draw.text(x, y, word) + x += word_width + spacing # Add word width and spacing to x + + else: + y += word_height + line_width = 0 + # crop the image height to the text height + imgw.crop(0, 0, img_width, y+spacing) + + draw(imgw) + img_bytes = imgw.make_blob(format='png') + st.image(img_bytes) + # st.image(img) if __name__ == "__main__":