Make writing easier!

Overview

Handwriter

Make writing easier!

How to

  1. Download and install a handwriting font, or create a font from your handwriting.

  2. Use a word processor like Microsoft Word or LibreOffice Writer to write to your heart's content with the handwriting font. (Hint: Use colors. #000000 for black, and #082A5E for blue works nicely.)

  3. Introduce some mistakes.

  4. Save each page as an image.

    • Save directly as an image. I use a size of (1626*2300) and a resolution of 50 pixels/cm (LibreOffice Writer).
    • Save as a PDF and convert to PNGs.
      pdftoppm -png pdf_file.pdf image_name -scale-to-x 1626 -scale-to-y 2300
  5. Transform the images to handwriting style. Requires numpy and OpenCV.

    python3 writing_artifact.py /path/to/image_name-*.png

    The images are saved (by default) in the ./out/ folder.

  6. Change the edited images back to PDF.

    convert image_name-*_edited.png pdf_edited.pdf

Simulating handwritten text

This is yet another project to create handwritten-style documents digitally. You may use this for assignments, among other things. Other similar projects/links include (in no particular order):

I prefer typing, like other like-minded individuals who might be interested in this project, or who know or uses the above links. And typing saves paper, saves trees, saves the planet; now I'm just being ambitious. What more, if you want to copy your answers from some source, not that I support cheating, typing is much easier (copy-paste).

Why another project?

This project was inspired by MyHandWriting, or rather, the developer's Reddit post. However, that, or other projects above support only plain text, without any formatting. For instance, what if you want a table or just multiple columns?

This project achieves this in two steps. First, the properly formatted text is written with a word processor, and each page is saved as an image. Then, the images are transformed in handwritten style with

python3 writing_artifact.py /path/to/pages.png

The code above simulates different handwriting artifacts and places the text on a white (A4, preferably) page. Several ideas were taken from the shortcomings of the above projects. This of course assumes that you don't have beautiful calligraphic handwriting.

Steps taken

The code uses numpy and OpenCV to do its job. Make sure to install them with pip. The following images were edited with GIMP, but the same thing is done with OpenCV in the code. The following images use the Homemade Apple font, but I use my handwriting as a font. Using your handwriting gives better results. This code also makes several assumptions based on my writing style.

Creating your font

If you want to use your handwriting as a font, use a site like Calligraphr. You may create a randomized font, but that is not necessary; the code will randomize the text. Once you've downloaded the font, use software like FontForge to edit the kerning or left and right bearing. I prefer editing the kerning. If your handwriting is cursive and continuous, this step is necessary.

To edit kerning for a font

  1. Open the font with FontForge.
  2. Go to Metrics > New Metrics Window.
  3. Go to Metrics > Window Type, make sure that Kerning only is selected.
  4. Click on the New Lookup Subtable... dropdown, click New Lookup Subtable...
    1. Under the Feature column in the popup, click on the dropdown beside . Select kern Horizontal Kerning.
    2. Click OK.
    3. Again click OK in the popup, rename the table if you want.
  5. Type some common words. Drag the characters or edit the values in the Kern field. Kerning font Type other words and adjust kerning. You don't need to do this for all letter pairs; when using the font, if you find some discrepancies, edit the kerning accordingly.
  6. Close the window.
  7. Go to File > Generate Fonts... Navigate to the folder where to save, choose the file name and Generate. You may ignore any warnings.

You may want to add other glyphs and tables to the font for ligatures, conditional substitution, etc. For instance, cursive uppercase letters not followed by a lowercase letter might look odd. In such cases, an alternate glyph might be suitable. As for me, I use a different font for these letters (see in Introducing mistakes).

Install the font. Clicking on the saved font will install it on most platforms. Re-edit the font file and (uninstall) reinstall when required.

Write

Use your favorite word processor to write whatever you want. Don't forget to use the font you just installed. Edit margins (usually reduce) to match your writing style. Make sure to use a white background and dark colors for writing. Do not use the red color as it is used to mark mistakes. The code makes these assumptions. If you're using tables, remove the borders. Change line spacing to 1.5 (recommended).

Introducing mistakes

Humans make spelling mistakes when writing, and strike out the mistakes. This is done by surrounding a misspelled word with a red border. The following steps add a red border in LibreOffice Writer.

  1. Select a misspelled word.
  2. Right click > Character > Character...
  3. Go to Borders.
  4. Select the second option in Presets: to add a border on all sides.
  5. In Padding, set everything to 0 (optional).
  6. Under Line in Color:, set to red (#FF0000).

There's also a LibreOffice macro available to perform these steps. The macro is limited, edit the code for variations.

  1. Copy paste the macro in ~/.config/libreoffice/4/user/Scripts/python/. Rename if you want.
  2. Restart LibreOffice if needed.
  3. Select all text where mistakes need to be inserted.
  4. Go to Tools > Macros > Organize Macros > Python...
  5. Expand My Macros > name_of_macro_that_you_copied, select introduceMistakes and click Run.
  6. This will randomly add misspelled words with a red border. This will also change the font for those uppercase letters which are not followed by a lowercase letter.

There are some known bugs/features.

  • It does not introduce mistakes in text inside objects, for instance, tables. For this, select the text inside each object (each table cell) and re-run the macro.
  • Sometimes (when there's an object at the end of a selection), you might see the INTRODUCEMISTAKEENDMARKER word. Delete it.

Make a menu entry for easier access to the macro.

Introduce artifacts

Save the document as a PDF or images. If PDF, convert the pages to images with your preferred method. Now pass the folder with all the images as an argument to the code. You may also pass each image individually, and/or use wildcards. Note that, for wildcards, the code relies on the shell to expand the file names.

The output images are saved as _edited.. By default, the image's original extension (format) is used. You can change the format with the -f or --output-format option. Since OpenCV is used to read and write image files, the extension must be one supported by your installation of OpenCV. By default, the output images are saved in the ./out/ directory. You can change this with the -o or --out option.

Use python3 writing_artifact.py -h for help. Relevant options will be discussed below.

Getting the mask

As discussed earlier, the mistakes in the document are marked with a red border. The first step is to get these red rectangles as a mask, and the image without the red color. Misspelling mask You may notice slight artifacts where the red lines were present, a little bit of the text is missing. This is because this font uses negative bearings to simulate the cursive overlap. This can be prevented slightly if kerning is used instead.

Preprocessing

The original text is slightly thin, but that's how my handwriting font is. So there's an extra preprocessing step, which thickens the letters slightly and reduces sharp edges.

Randomizing text

As of now, all the letters look the same. However, in handwriting, there's some variation in every letter. One way is to use a randomized font. Alternately, we can randomly move the text a little with a noise map. Randomized text There's two noise map here, one for each axis. The amount to move the text with these maps can be controlled with the -r option; 0 means no movement, other values move in the positive or negative direction. The scale of the noise map can be controlled with the -s option; higher values mean a higher frequency of the noise. Experiment with this to match your font size.

Striking the mistakes

Using the mask, some strikes are generated randomly and applied on top of the mistakes. The color of the strikes is automatically extracted from the surrounding texts. Misspelling strike The original strikes are generated with inverted colors for easier processing. Inverted not in the usual way how colors are inverted, but by taking the bitwise not. The strikes are also cropped with a convex hull of the text.

Getting lines of text

We go through each row of pixels in the image and check if it only consists of white rows or other colors too. This is used to divide the image into alternating white and text areas. We also select some of the lines based on their length, line spacing, etc. White rows The red lines show the rows, the blue lines are the selected ones. Sometimes, if two lines are too close, they might be detected as a single line. Try changing the line-height if so.

Change line spacing

In handwriting, we tend to have some lines closer to each other than others. In other words, we have varying line spacing. Using the rows of text found above, we move each line randomly up or down. Line spacing The amount to move the lines in this step can be controlled with the -t option; 0 means no movement, other values move in the positive or negative direction.

Slanting lines

When writing, we tend to slant the lines (generally upwards). We select blocks of text based on the selected lines above (blue lines) and slant each of these blocks separately. Several different methods to slant the text is available and one of them is chosen randomly. For each of the blocks, either no slanting is done, or

  1. Skew all the lines together. Skewed lines
  2. Skew each line by a different amount. The first line is not skewed, the second line is skewed a little, the third line is skewed a little more, and so on. Partially skewed lines
  3. Fake perspective transforms to shorten the right side of the text. Fake perspective transform

What's a fake perspective?

With a normal perspective transform, we get the following. Perspective transform Note how the columns on the top move to the right. This is not an error, but the expected behavior of perspective transform. However, we can't have this in our case since this will mess up column alignments, for instance, in tables.

Instead, we use a vertical displacement map like the following to shrink the right side of the block. Vertical displacement map This generates the slants as shown above with proper column alignment. The amount to slant the lines in this step can be controlled with the -k option; 0 means no movement, other values move in the positive (upward) or negative (downward) direction.

Backgrounds

Once we've sufficiently applied handwriting effects to the text, we need to place it on top of an (A4) paper texture. By default, the background images are read from the ./background folder. You can change this with the -b or --background option.

To use your backgrounds, scan or take a picture of a white A4 paper. Use image editing software like GIMP to crop the image such that the page fills the whole image. You might also need to apply some perspective transformation and/or cage transformations to fit the image. Without this, the curves in the paper will make the output look unnatural. Place the edited images in your background folder.

Reading backgrounds

When a background image is read, some optional transformations are randomly applied to generate variations of the background. Background transformations The original image and three other variations can be combined to produce 8 different backgrounds from a single background. Moreover, multiple backgrounds can be merged to generate even more backgrounds. Each of those backgrounds can have its variations. This allows generating a wide number of backgrounds from only a few original ones.

When multiple images are passed to the code at once, one background for each of them is generated at a time while ensuring that each of them is unique.

Merging text on background

Once the background is read, it might have shadows. These shadows are used to further displace the image to give the effect that the text curves with the page. Background displacement Finally, the text is merged on the background, normalized, and saved. Final image Here's how it looks before and after. If you use your handwriting, it'll look more realistic.

TODO

  1. Detect images (line drawing) and apply low-frequency noise to them to give a hand-drawn effect.
  2. Better strike out for mistakes.
  3. Second page's background will be the flipped variant of the first.
  4. The second page will have flipped bleed through text from the first page.
  5. Each line will have random horizontal movement.
An anthology of a variety of tools for the Persian language in Python

An anthology of a variety of tools for the Persian language in Python

Persian Tools 106 Nov 08, 2022
split Word file by chapter

split Word file by chapter we use the mircosoft word api to code this tool api url:https://docs.microsoft.com/zh-cn/dotnet/api/ if this tool is good f

wisdom under lemon trees 5 Nov 06, 2021
Chilean Digital Vaccination Pass Parser (CDVPP) parses digital vaccination passes from PDF files

cdvpp Chilean Digital Vaccination Pass Parser (CDVPP) parses digital vaccination passes from PDF files Reads a Digital Vaccination Pass PDF file as in

Esteban Borai 1 Nov 17, 2021
Fuzz a language by mixing up only few words.

afasi Fuzz a language by mixing up only few words. Status Beta. Note: The default branch is default. Use Examples Version General Help Translate Help

Stefan Hagen 2 Dec 14, 2022
Returns unicode slugs

Python Slugify A Python slugify application that handles unicode. Overview Best attempt to create slugs from unicode strings while keeping it DRY. Not

Val Neekman 1.3k Jan 04, 2023
A production-ready pipeline for text mining and subject indexing

A production-ready pipeline for text mining and subject indexing

UF Open Source Club 12 Nov 06, 2022
Migrates translations to the REDCap native Multi-Language Management system

Automates much of the process of moving translations from the old Multilingual external module to the newer built-in Multi-Language Management (MLM) page.

UCI MIND 3 Sep 27, 2022
The Scary Story - A Text Adventure

This is a text adventure which I made in python 3. This is one of my first big projects so any feedback would be greatly appreciated.

2 Feb 20, 2022
CowExcept - Spice up those exceptions with cowexcept!

CowExcept - Spice up those exceptions with cowexcept!

James Ansley 41 Jun 30, 2022
Split large XML files into smaller ones for easy upload

Split large XML files into smaller ones for easy upload. Works for WordPress Posts Import and other XML files.

Joseph Adediji 1 Jan 30, 2022
一个可以可以统计群组用户发言,并且能将聊天内容生成词云的机器人

当前版本 v2.2 更新维护日志 更新维护日志 有问题请加群组反馈 Telegram 交流反馈群组 点击加入 演示 配置要求 内存:1G以上 安装方法 使用 Docker 安装 Docker官方安装

机器人总动员 117 Dec 29, 2022
A minimal code sceleton for a textadveture parser written in python.

Textadventure sceleton written in python Use with a map file generated on https://www.trizbort.io Use the following Sockets for walking directions: n

1 Jan 06, 2022
A query extract python package

A query extract python package

Fayas Noushad 4 Nov 28, 2021
ChirpText is a collection of text processing tools for Python 3.

ChirpText is a collection of text processing tools for Python 3. It is not meant to be a powerful tank like the popular NTLK but a small package which

Le Tuan Anh 5 Nov 30, 2022
Free & simple way to encipher text

VenSipher VenSipher is a free medium through which text can be enciphered. It can convert any text into an unrecognizable secret text that can only be

3 Jan 28, 2022
Text Summarizationcls app with python

Text Summarizationcls app This is the repo for the Text Summarization AI Project. It makes use of pre-trained Hugging Face models Packages Used The pa

Edem Gold 1 Oct 23, 2021
Make writing easier!

Handwriter Make writing easier! How to Download and install a handwriting font, or create a font from your handwriting. Use a word processor like Micr

64 Dec 25, 2022
Vastasanuli - Vastasanuli pelaa Sanuli-peliä.

Vastasanuli Vastasanuli pelaa SANULI -peliä. Se ei aina voita. Käyttö Tarttet Pythonin (3.6+). Aja make (tai lataa words.txt muualta) Asentele vaaditt

Aarni Koskela 1 Jan 06, 2022
Hotpotato is a recipe portfolio App that assists users to discover and comment new recipes.

Hotpotato Hotpotato is a recipe portfolio App that assists users to discover and comment new recipes. It is a fullstack React App made with a Redux st

Nico G Pierson 13 Nov 05, 2021
Python flexible slugify function

awesome-slugify Python flexible slugify function PyPi: https://pypi.python.org/pypi/awesome-slugify Github: https://github.com/dimka665/awesome-slugif

Dmitry Voronin 471 Dec 20, 2022