madpeset/txt.py

130 lines
3.9 KiB
Python
Raw Normal View History

2024-01-07 12:57:31 +02:00
# -*- coding: utf-8 -*-
2024-01-09 10:23:22 +02:00
import os
2023-09-20 07:28:09 +03:00
import logging
import argparse
2023-09-20 04:25:27 +03:00
from PIL import Image, ImageDraw, ImageFont
import subprocess
2024-01-07 12:29:14 +02:00
2023-09-20 07:28:09 +03:00
# Initialize logging
logging.basicConfig(filename='your_log_file.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
2023-09-20 04:25:27 +03:00
2024-01-09 10:23:22 +02:00
# Get the current user's username
username = os.getlogin()
2023-09-20 07:28:09 +03:00
# CLI argument parsing
parser = argparse.ArgumentParser(description="Image Generation and Printing")
args = parser.parse_args()
def camera():
print("cam")
2024-01-09 10:23:22 +02:00
webcam_command = "fswebcam webcam.jpg ; /home/{username}/.local/bin/brother_ql -b pyusb -m QL-550 -p usb://0x04f9:0x2016 print -l 62 --dither webcam.jpg"
2023-09-20 07:28:09 +03:00
subprocess.run(webcam_command, shell=True)
2024-01-07 12:29:14 +02:00
def map_to_keyboard_hebrew(input_text):
keyboard_to_hebrew = {
't': 'א',
'c': 'ב',
'd': 'ג',
's': 'ד',
'v': 'ה',
'u': 'ו',
'z': 'ז',
'j': 'ח',
'y': 'ט',
'h': 'י',
'f': 'כ',
'k': 'ל',
'n': 'מ',
2024-01-07 12:38:46 +02:00
'o': 'ם',
2024-01-07 12:29:14 +02:00
'b': 'נ',
2024-01-07 12:38:46 +02:00
'i': 'ן',
2024-01-07 12:29:14 +02:00
'x': 'ס',
'g': 'ע',
'p': 'פ',
'm': 'צ',
2024-01-07 12:38:46 +02:00
'.': 'ץ',
2024-01-07 12:29:14 +02:00
'e': 'ק',
'r': 'ר',
'a': 'ש',
',': 'ת',
';': 'ף',
'i': 'ן',
'l': 'ך',
2024-01-07 12:38:46 +02:00
'/': '.',
2024-01-07 12:57:31 +02:00
'\/': ',',
'@': '"',
2023-09-20 07:28:09 +03:00
# Add any other characters you want to map
}
2024-01-07 12:29:14 +02:00
output_text = ''.join([keyboard_to_hebrew.get(char, char) for char in input_text.lower()])
2023-09-20 07:28:09 +03:00
return output_text
2023-09-20 04:25:27 +03:00
2024-01-07 12:29:14 +02:00
def rtl_text_wrap(text, width):
words = text.split()
lines = []
current_line = []
current_line_length = 0
2024-01-07 12:44:23 +02:00
for word in words:
2024-01-07 12:29:14 +02:00
if current_line_length + len(word) <= width:
2024-01-07 12:44:23 +02:00
current_line.insert(0, word) # Insert word at the beginning of the current line
current_line_length += len(word) + 1
2024-01-07 12:29:14 +02:00
else:
2024-01-07 12:44:23 +02:00
# When the line exceeds the width, finalize the current line
2024-01-07 12:38:46 +02:00
lines.append(' '.join(current_line))
2024-01-07 12:29:14 +02:00
current_line = [word]
2024-01-07 12:38:46 +02:00
current_line_length = len(word) + 1
2024-01-07 12:29:14 +02:00
2024-01-07 12:44:23 +02:00
# Add the last line to the lines list
lines.append(' '.join(current_line))
2024-01-07 12:29:14 +02:00
return '\n'.join(lines)
2024-01-07 12:38:46 +02:00
2024-01-07 12:44:23 +02:00
2023-09-20 04:25:27 +03:00
while True:
user_input = input("Type something (or press Enter to generate image): ")
2023-09-20 07:28:09 +03:00
logging.info(f"User input: {user_input}")
if user_input =="+":
camera()
elif user_input != "":
2024-01-07 12:29:14 +02:00
img_width = 696
# Step 1: Map to Hebrew
mapped_text = map_to_keyboard_hebrew(user_input)
print("mapped "+mapped_text)
logging.info("Generating image...")
# Reverse characters within each word
reversed_within_words = ' '.join([word[::-1] for word in mapped_text.split()])
wrapped_text = rtl_text_wrap(reversed_within_words, 8)
2023-09-20 07:28:09 +03:00
2024-01-07 12:29:14 +02:00
print("warp: "+wrapped_text)
2023-09-20 04:25:27 +03:00
num_lines = wrapped_text.count('\n') + 1
2023-09-20 07:28:09 +03:00
font_size = img_width // 6
2024-01-09 10:23:22 +02:00
ttfont="5x5-Tami.ttf"
ttfont="VarelaRound-Regular.ttf"
ttfont="fonts/xbmc-hebrew-fonts/Roboto-Bold-xbmc-il.ttf"
2023-09-20 04:25:27 +03:00
font = ImageFont.truetype(ttfont, font_size)
2023-09-20 07:28:09 +03:00
line_height = font_size + 10
2023-09-20 04:25:27 +03:00
img_height = num_lines * line_height + 200
image = Image.new('RGB', (img_width, img_height), 'white')
d = ImageDraw.Draw(image)
text_width, text_height = d.textsize(wrapped_text, font=font)
x = (img_width - text_width) // 2
y = (img_height - text_height) // 2
2024-01-07 12:29:14 +02:00
2023-09-20 04:25:27 +03:00
d.text((x, y), wrapped_text, font=font, fill=(0, 0, 0))
image.save("output.png")
2024-01-07 12:29:14 +02:00
2023-09-20 04:25:27 +03:00
printer_ql550="0x2016"
printer_id1="000M6Z401370"
2024-01-09 10:23:22 +02:00
command = f"/home/{username}/.local/bin/brother_ql -b pyusb --model QL-550 -p usb://0x04f9:{printer_ql550}/{printer_id1} print -l 62 output.png"
2023-09-20 04:25:27 +03:00
subprocess.run(command, shell=True)
2023-09-20 07:28:09 +03:00
# Uncomment this line if you'd like to listen for arrow key events.
# keyboard.on_press_key("up", on_arrow_key)