# Text to Handwriting Converter

This utility converts digital text into an image that resembles handwriting using the `pywhatkit` library. It's a fun tool for generating handwritten-style notes or documents from typed text.

**Modules Used:**
*   `pywhatkit`: A library with various automation features, including text-to-handwriting conversion.
*   [[programming/python/modules/argparse-module|argparse]]: To handle command-line arguments.

## Installation

```bash
pip install pywhatkit
```

## The Code

Save this as `handwriting.py`.

```python
import pywhatkit
import argparse
import os
import sys

def convert_to_handwriting(text, output_file, color):
    try:
        print(f"Converting text to handwriting...")
        
        # Parse RGB color
        try:
            rgb = tuple(map(int, color.split(',')))
            if len(rgb) != 3:
                raise ValueError
        except ValueError:
            print("Error: Color must be in R,G,B format (e.g., 0,0,0)")
            return

        # Convert
        # save_to: Path to save the image
        # rgb: Color of the handwriting in (R, G, B) format
        pywhatkit.text_to_handwriting(text, save_to=output_file, rgb=rgb)
        
        print(f"Success! Image saved to '{output_file}'")
        
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Text to Handwriting Converter")
    parser.add_argument("input", help="Text to convert OR path to a text file")
    parser.add_argument("-o", "--output", default="handwriting.png", help="Output image filename (default: handwriting.png)")
    parser.add_argument("--color", default="0,0,138", help="RGB Color (default: 0,0,138 for dark blue)")
    
    args = parser.parse_args()
    
    # Check if input is a file path
    text_data = args.input
    if os.path.isfile(args.input):
        try:
            with open(args.input, 'r', encoding='utf-8') as f:
                text_data = f.read()
            print(f"Read content from file: {args.input}")
        except Exception as e:
            print(f"Error reading file: {e}")
            sys.exit(1)

    convert_to_handwriting(text_data, args.output, args.color)
```

## Usage

```bash
# Convert a simple string
python handwriting.py "Hello, this is handwritten text."

# Convert a text file and save as blue text
python handwriting.py notes.txt --output my_notes.png --color 0,0,255
```

[[programming/python/python]]