Generate text images for training deep learning ocr model
Branch: master
Clone or download
Sanster Merge pull request #20 from xiaomaxiao/master
set encoding defalut = 'utf-8',
because windows default to gbk encoding
Latest commit b1d5974 Nov 1, 2018

Text Renderer

Generate text images for training deep learning OCR model (e.g. CRNN). Support both latin and non-latin text.


  • Ubuntu 16.04
  • python 3.5+

Install dependencies:

pip3 install -r requirements.txt


By default, simply run python3 will generate 20 text images and a labels.txt file in output/default/.

example1.jpg example2.jpg

example3.jpg example4.jpg

Use your own data to generate image

  1. Please run python3 --help to see all optional arguments and their meanings. And put your own data in corresponding folder.

  2. Config text effects and fraction in configs/default.yaml file(or create a new config file and use it by --config_file option), here are some examples:

Effect name Image
Origin(Font size 25) origin
Perspective Transform perspective
Random Crop rand_crop
Curve curve
Light border light border
Dark border dark border
Random char space big random char space big
Random char space small random char space small
Middle line middle line
Table line table line
Under line under line
Emboss emboss
Reverse color reverse color
Blur blur
  1. Run file.

Strict mode

For no-latin language(e.g Chinese), it's very common that some fonts only support limited chars. In this case, you will get bad results like these:




Select fonts that support all chars in --chars_file is annoying. Run with --strict option, renderer will retry get text from corpus during generate processing until all chars are supported by a font.


You can use script to check how many chars your font not support in --chars_file:

python3 tools/

checking font ./data/fonts/eng/Hack-Regular.ttf
chars not supported(4971):
['', '', '广', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '','', '', '', ''...]
0 fonts support all chars(5071) in ./data/chars/chn.txt:

Generate image using GPU

If you want to use GPU to make generate image faster, first compile opencv with CUDA. Compiling OpenCV with CUDA support

Then build Cython part, and add --gpu option when run

cd libs/gpu
python3 build_ext --inplace

Debug mode

Run python3 --debug will save images with extract information. You can see how perspectiveTransform works and all bounding/rotated boxes.