Write me a Python script that gets a text file as argument and writes to another text…
- User
Write me a Python script that gets a text file as argument and writes to another text file and does this:
- replaces every "HBASA-ESASA DOTTED" or "U+073C" under a ܒܓܕܟܦܬ letter with "RUKKAKHA" or "U+0742"
- replaces every "RWAHA" with "QUSHSHAYA" on ܒܓܕܟܦܬ
- Assistant
Certainly! Below is a Python script that takes a text file as an argument, processes it according to the specified rules, and writes the results to a new text file.
Make sure you have Python installed on your system, and follow these steps:
- Save the following script into a file named
text_replacer.py:
import sys import re def replace_text(input_file, output_file): with open(input_file, 'r', encoding='utf-8') as infile: contents = infile.read() # Replace "HBASA-ESASA DOTTED" or "U+073C" under a ܒܓܕܟܦܬ letter with "RUKKAKHA" or "U+0742" # The regex looks for any of the ܒ, ܓ, ܕ, ܟ, ܦ, ܬ followed by the specific phrases letter_pattern = r'([\u0722-\u072A])' contents = re.sub(r'({} )?(HBASA-ESASA DOTTED|U\+073C)'.format(letter_pattern), r'\1RUKKAKHA', contents) # Replace every "RWAHA" with "QUSHSHAYA" on ܒܓܕܟܦܬ contents = re.sub(r'({} )?RWAHA'.format(letter_pattern), r'\1QUSHSHAYA', contents) with open(output_file, 'w', encoding='utf-8') as outfile: outfile.write(contents) if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python text_replacer.py <input_file> <output_file>") else: input_file = sys.argv[1] output_file = sys.argv[2] replace_text(input_file, output_file) print(f"Processed {input_file} and wrote to {output_file}.") - Save the following script into a file named
- User
You didn't do it like you should: RWAHA is U+073F in Unicode, is not a string, QUSHSHAYA is U+0741 in Unicode, is not a string, HBASA-ESASA DOTTED is U+073C in Unicode, not a string and RUKKAKHA is U+0742 in Unicode not a string!
- Assistant
I apologize for the misunderstanding regarding the Unicode representations. Let's refine the script based on your clarification. We will directly work with the Unicode character representations instead of treating them as strings.
Here's the updated Python script:
import sys import re def replace_text(input_file, output_file): # Define the Unicode characters hbasa_esasa_dotted = '\u073C' # U+073C rwuha = '\u073F' # U+073F rukakkha = '\u0742' # U+0742 qushshaya = '\u0741' # U+0741 letters_pattern = r'[\u0722-\u072A]' # ܒܓܕܟܦܬ with open(input_file, 'r', encoding='utf-8') as infile: contents = infile.read() # Replace "HBASA-ESASA DOTTED" or "U+073C" under a ܒܓܕܟܦܬ letter with "RUKKAKHA" or "U+0742" contents = re.sub( rf'({letters_pattern}) ?({hbasa_esasa_dotted})', rf'\1{rukakkha}', contents ) # Replace every "RWAHA" (U+073F) with "QUSHSHAYA" (U+0741) on ܒܓܕܟܦܬ contents = re.sub( rf'({letters_pattern}) ?({rwuha})', rf'\1{qushshaya}', contents ) with open(output_file, 'w', encoding='utf-8') as outfile: outfile.write(contents) if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python text_replacer.py <input_file> <output_file>") else: input_file = sys.argv[1] output_file = sys.argv[2] replace_text(input_file, output_file) print(f"Processed {input_file} and wrote to {output_file}.") - User
The script doesn't work. It should change ܟܬܼܳܒܼܳܐ ܕܺܝܠܺܝܕܼܽܘܬܼܶܗ ܕܝܶܫܽܘܥ ܡܫܺܝܚܳܐ܃ ܒܪܶܗ ܕܕܼܰܘܺܝܕܼ܂ ܒܪܶܗ ܕܰܐܒܼܪܳܗܳܡ܀܀ to ܟܬ݂ܳܒ݂ܳܐ ܕܺܝܠܺܝܕ݂ܽܘܬ݂ܶܗ ܕܝܶܫܽܘܥ ܡܫܺܝܚܳܐ܃ ܒܪܶܗ ܕܕ݂ܰܘܺܝܕ݂܂ ܒܪܶܗ ܕܰܐܒ݂ܪܳܗܳܡ܀܀ but the output is: ܟܬܼܳܒܼܳܐ ܕܺܝܠܺܝܕܼܽܘܬܼܶܗ ܕܝܶܫܽܘܥ ܡܫܺܝܚܳܐ܃ ܒܪܶܗ ܕܕܼܰܘܺܝܕܼ܂ ܒܪܶܗ ܕܰܐܒܼܪܳܗܳܡ܀܀
- Assistant
- User
I have this python code but I got an error: from PIL import ImageFont, ImageDraw, Image from luma.core.render import canvas
def find_font_size(text, font, image, target_width_ratio): tested_font_size = 100 tested_font = ImageFont.truetype(font, tested_font_size) observed_width, observed_height = get_text_size(text, image, tested_font) estimated_font_size = tested_font_size / (observed_width / image.width) * target_width_ratio return round(estimated_font_size)
def get_text_size(text, image, font): im = Image.new('RGB', (image.width, image.height)) return draw.textsize(text, font)
width_ratio = 0.5 font_family = "fonts_syriac/syrcomjerusalem.ttf" text = "ܟܬܼܳܒܼܳܐ"
image = Image.open('newtestamentinsy0000unse_g7r6_0242.jpg') editable_image = ImageDraw.Draw(image) font_size = find_font_size(text, font_family, image, width_ratio) font = ImageFont.truetype(font_family, font_size) print(f"Font size found = {font_size} - Target ratio = {width_ratio} - Measured ratio = {get_text_size(text, image, font)[0] / image.width}")
editable_image.text((10, 10), text, font=font) image.save('output.png')
- Assistant
- User
Traceback (most recent call last): File "/home/incognito/dataset2image/a.py", line 22, in font_size = find_font_size(text, font_family, image, width_ratio) File "/home/incognito/dataset2image/a.py", line 7, in find_font_size observed_width, observed_height = get_text_size(text, image, tested_font) File "/home/incognito/dataset2image/a.py", line 14, in get_text_size return draw.textsize(text, font) AttributeError: 'ImageDraw' object has no attribute 'textsize'
- Assistant
- User
Any idea how can I get the font size from an image of a book page?
- Assistant
- User
add some arguments to this script: "input image", "text", "font path", "output image" - text to write, font path for the font to use, output image where to write the thext with that font with the Estimated font size
- Assistant
- User
Traceback (most recent call last): File "/home/incognito/dataset2image/fs.py", line 78, in write_text_on_image(args.input_image, args.text, args.font_path, args.output_image) File "/home/incognito/dataset2image/fs.py", line 54, in write_text_on_image text_width, text_height = draw.textsize(text, font=font) AttributeError: 'ImageDraw' object has no attribute 'textsize'
- Assistant
- User
I don't see the text one the image, please make a red bounding box around it
- Assistant
- User
The written text is way smaller than the original text in the image
- Assistant