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.
Simple python program to auto credit your code, text, book, whatever!

Credit Simple python program to auto credit your code, text, book, whatever! Setup First change credit_text to whatever text you would like to credit

Hashm 1 Jan 29, 2022
Export solved codewars kata challenges to a text file.

Codewars Kata Exporter Note:this is not totally my work.i've edited the project to make more easier and faster for me.you can find the original work h

Oussama Ben Sassi 4 Aug 13, 2021
A generator library for concise, unambiguous and URL-safe UUIDs.

Description shortuuid is a simple python library that generates concise, unambiguous, URL-safe UUIDs. Often, one needs to use non-sequential IDs in pl

Stavros Korokithakis 1.8k Dec 31, 2022
Making simplex testing clean and simple

Making Simplex Project Testing - Clean and Simple What does this repo do? It organizes the python stack for the coding project What do I need to do in

Mohit Mahajan 1 Jan 30, 2022
Auto translate Localizable.strings for multiple languages in Xcode

auto_localize Auto translate Localizable.strings for multiple languages in Xcode Usage put your origin Localizable.strings file in folder pip3 install

Wesley Zhang 13 Nov 22, 2022
Wordle strategy: Find frequency of letters appearing in 5-letter words in the English language

Find frequency of letters appearing in 5-letter words in the English language In

Gabriel Apolinário 1 Jan 17, 2022
从flomo导出的笔记中生成词云

flomo-word-cloud 从flomo导出的笔记中生成词云 如何使用? 将本项目克隆到你的电脑上,使用如下的命令,安装所需python库 pip install -r requirements.txt 在项目里新建一个file文件夹,把所有从flomo导出的html文件放入其中 运行main

Hannnk 9 Dec 30, 2022
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
a python package that lets you add custom colors and text formatting to your scripts in a very easy way!

colormate Python script text formatting package What is colormate? colormate is a python library that lets you add text formatting to your scripts, it

Rodrigo 2 Dec 14, 2022
Hamming code generation, error detection & correction.

Hamming code generation, error detection & correction.

Farhan Bin Amin 2 Jun 30, 2022
Convert text to morse code and play morse code sound.

Convert text(english) to morse codes and play morse sound!

Mohammad Dori 5 Jul 15, 2022
Implementation of hashids (http://hashids.org) in Python. Compatible with Python 2 and Python 3

hashids for Python 2.7 & 3 A python port of the JavaScript hashids implementation. It generates YouTube-like hashes from one or many numbers. Use hash

David Aurelio 1.4k Jan 02, 2023
Extract price amount and currency symbol from a raw text string

price-parser is a small library for extracting price and currency from raw text strings.

Scrapinghub 252 Dec 31, 2022
A simple Python module for parsing human names into their individual components

Name Parser A simple Python (3.2+ & 2.6+) module for parsing human names into their individual components. hn.title hn.first hn.middle hn.last hn.suff

Derek Gulbranson 574 Dec 20, 2022
An extension to detect if the articles content match its title.

Clickbait Detector An extension to detect if the articles content match its title. This was developed in a period of 24-hours in a hackathon called 'H

Arvind Krishna 5 Jul 26, 2022
Maiden & Spell community player ranking based on tournament data.

MnSRank Maiden & Spell community player ranking based on tournament data. Why? 2021 just ended and this seemed like a cool idea. Elo doesn't work well

Jonathan Lee 1 Apr 20, 2022
汉字转拼音(pypinyin)

汉字拼音转换工具(Python 版) 将汉字转为拼音。可以用于汉字注音、排序、检索(Russian translation) 。 基于 hotoo/pinyin 开发。 Documentation: http://pypinyin.rtfd.io/ GitHub: https://github.co

Huang Huang 4.2k Jan 03, 2023
知乎评论区词云分析

zhihu-comment-wordcloud 知乎评论区词云分析 起源于:如何看待知乎问题“男生真的很不能接受彩礼吗?”的一个回答下评论数超8万条,创单个回答下评论数新记录? 项目代码说明 2.download_comment.py 下载全量评论 2.word_cloud_by_dt 生成词云 2

李国宝 10 Sep 26, 2022
Production First and Production Ready End-to-End Keyword Spotting Toolkit

WeKws Production First and Production Ready End-to-End Keyword Spotting Toolkit. The goal of this toolkit it to... Small footprint keyword spotting (K

222 Dec 30, 2022
Vector space based Information Retrieval System for Text Processing - Information retrieval

Information Retrieval: Text Processing Group 13 Sequence of operations Install Requirements Add given wikipedia files to the corpus directory. Downloa

1 Jan 01, 2022