Total Pageviews

Tuesday 30 June 2020

Handwriter

A lightweight tool for you to generate handwriting-like text for printing.

Handwriter-python3 Features

  • Generate practically real handwriting-liked texts to .webp directly from text file for printing
  • Simulating handwriting is compatible with Chinese and almost all languages with Unicode encoded
  • Custom handwriting font supported and the whole of each word should be randomly disturbed including its stroke rotation angle
  • Three degrees of freedom transform supported. Horizontal position, vertical position and font size are randomly disturbed
  • Easy to use and cross-platform and custom json configuration supported

Handwriter-python3 Test Environment

  • macOS 10.15 with Python 3.7.4
  • Windows 10 20H1 Professional with Python 3.7.4 (x64)
  • Ubuntu 19.10 with Python 3.6.9 (64bit)

Handwriter-python3 Usage

Firstly, you have to download handwriter.py or copy its source code to you IDE. If you want to use custom configuration or font presets, just clone Handwriter directly:
gitclone https://github.com/Unbinilium/Handwriter.git
Secondly, Install dependencies that Handwriter-python3 required:
pip3 install uuid pillow twine setuptools wheel tox pytest handright
Thirdly, enter Handwriter path and run handwriter.py by python3 handwriter.py, follow the tips displayed in the console. It generates output file .webp using default configuration.
Last but not least, to avoid some rare errors, making sure your text file's name is *.txt and both name and text are encoded with UTF-8. For custom fonts please use TrueTypeFont file which extension is .ttf, and I recommend to use handwriting-liked fonts by default. Remmber outfilepath is a directory path but not a file path, the outfilename is automatically generated by uuid.
For further customization, please edit the Handwriter-python3 configuration file named handwriter.json manually, the default example is here:
{
    "text_file_path":"~/*.txt",
    "font_file_path":"~/*.ttf",
    "output_path":"~/",
    "background_color":"white",
    "background_width":2480,
    "background_hight":3500,
    "font_color":"black",
    "font_size":80,
    "font_size_sigma":2,
    "perturb_x_sigma":2,
    "perturb_y_sigma":2,
    "perturb_theta_sigma":0.05,
    "line_spacing":80,
    "line_spacing_sigma":3,
    "word_spacing":-30,
    "word_spacing_sigma":3,
    "left_margin":150,
    "top_margin":150,
    "right_margin":150,
    "bottom_margin":150,
    "end_chars":",。》?;:’”】}、!%),.>?;:]}!%)′″℃℉"
}
All the *_path should be specialized. Then run handwriter.py with argument configuration file path by python3 handwriter.py .
The basic type settings parameters definition are here. For strengthen text sharpness and clarity, using large paper size(background_widthbackground_hight) and big font_size is always recommended. Please notice the parameter line_spacing should always larger than font_size. If you considering the word spacing is too big, set it to a negative number.
 -  ______________________________________________________
 ^  | Paper                             ^                |
 |  |                   word_spacing    | top_margin     |
 b  | left_margin        |< --- >|      v                |
 a  | < --- > |-----------------------------| -          |
 c  |       - ------------       ------------ ^          |
 k  |       ^ | ******** |       | ******** | | line_    |
 g  | font_ | |    **    |       | **    ** | |          |
 r  | size  | |    **    |       | **    ** | | spacing  |
 o  |       v |    **    |       | ******** | v          |
 u  |       - ------------       ------------ -          |
 n  |         |-----------------------------|< -------- >|
 d  |         ------------                  right_margin |
|   |         | ******** |                               |
 h  |         | ***   ** |                               |
 i  |         | **       |                               |
 g  |         | **  char |                               |
 h  |         ------------                               |
 t  |             ^                                      |
 |  |             | bottom_margin                        |
 v  |             v                                      |
 -  |____________________________________________________|
    |<------- -------="" background_width="">|
Normally parameter font_color is set to black and background_color is set to white, using RGBA color profile and feel free to set color by specializing the color name . Also the background_color could be a picture file path, in this case background_colorstands for a background picture file path, try this if you want to use a custom background image.
The parameter *_sigma means random disturbing range and is always determined by the handwriting-liked font you have used, always change it to which perfectly matches your custom font manually. For example "font_size_sigma":2 means randomly disturbing character's size in range font_size ± 2. Similarly "perturb_theta_sigma":0.05 means randomly disturbing character stroke rotation radius in range horizontal ± 0.05. To avoid some special character like ,。》?;:’”】}、!%),.>?;:]}!%)′″℃℉ appear at the line indent, which is not following generally writing conventions which looks strange, specialize parameter end_chars is required.

Handwriter-cpp Features

  • Generate handwriting-liked text for printing from text file
  • Simulating Multiple ng handwriting is compatible with Chinese and almost all languages with Unicode encoded
  • Multiple custom handwriting font supported and each character's font is randomly generated
  • Each character size is randomly disturbed and margin in vertical position is randomly generated
  • Written in C++ which provides epic performance and Export as HTML by default with standard HTML API

Handwriter-cpp Test Environment

  • macOS 10.15 with XCode 11.2 and Safari
  • Windows 10 20H1 Professional with Visual Studio Community 2019 and Chrome Version 78

Handwriter-cpp Usage

Firstly, you have to download handwriter.cpp or copy its source code to you IDE. If you want to use the default font presets, just clone Handwriter directly:
gitclone https://github.com/Unbinilium/Handwriter.git
Secondly, please run handwriter.cpp with arguments configured, and arguments should follow the template example below:
handwriter.cpp     ... 
Each argument is split by space and follow the example sequence, text file in could not be empty and  should contain the file name with file extension .html, fonts path number is limited by int size. Be careful the handwriter requires at least 1 font path.
For example we can use 4 custom fonts to simulating handwriting from /usr/local/example.txt:
handwriter.cpp "/handwriter-master/example.txt" "/handwriter-master/example.html" "/handwriter-master/font/font1.ttf" "/handwriter-master/font/font2.otf" "/handwriter-master/font/font3.woff" "https://fonts.gstatic.com/example.woff2"
It simply convert example.txt to example.html with handwriting-liked font style, so it's convenient to use different types fonts which only determined by your browser. Also the path format is flexible, local path or URL are both ok. Otherwise, I recommend to use handwriting-liked fonts by default. For further customization like HTML TitleFont Size and Margin Space, edit the #define in handwriter.cpp:
#define HTML_TITLE         "Handwriter"
#define FONT_SIZE_MIN      21
#define FONT_SIZE_MAX      25
#define FONT_SIZE_PRECISON 0.1
#define MARGIN_MIN         5.0
#define MARGIN_MAX         5.5
#define MARGIN_PRECISION   0.01
Lastly double click to open the generated HTML in your browser and print it with your printer, fake handwriting got!

What's more

Currently handwriter-cpp is only a very simple idea and in a very low level of completion, it actually with a very clumsy algorithm. For whom like your teacher may see through your trick easily.
For more really handwriting generation, more degrees of freedom transform is required, such as horizontal position, vertical position and font size. And the whole of each word should be randomly disturbed including its stroke rotation angle. Also random but not temporarily random, algorithm for pen pressure, nit move speed and so on is required. And it better to use mechanical arm to simulating handwriting instead of printer.
I wrote this program just for fun and learn characters encode principle deeper by practicing. Obviously to make this perfect, using C++ and HTML is not a good idea, please try Handwriter-python3 instead.

No comments:

Post a Comment