Skip to content

Instantly share code, notes, and snippets.

@mylamour
Last active May 24, 2017 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mylamour/537f8a72dedd8803f57cba1b6365f5ef to your computer and use it in GitHub Desktop.
Save mylamour/537f8a72dedd8803f57cba1b6365f5ef to your computer and use it in GitHub Desktop.
generate chinese character, 生成汉字
#!/usr/bin/env bash
for font in $(ls -d $PWD/font/**)
do
rm -rf ./tmp_img
echo "Now we process font file in $font "
python3 gen_chinese_character_img.py $font
null_img_size=`find ./tmp_img -type f -exec ls -lSd {} + | awk '{print $5}' | uniq | tail -n 1`
count="ls ./tmp_img | wc -l"
o_count=`eval $count`
echo "All generate file is $o_count Now we delete the broken image file"
echo "The broken file size is $null_img_size"
find ./tmp_img -type f -size "$null_img_size"c -exec rm -f {} \; # attention not +sizec
p_count=`eval $count`
echo "Now the clean generate file is $p_count"
echo "Now We convert it to tif format...."
cd ./tmp_img
for png in $(ls)
do
name=`echo $png | awk -F "." '{print $1}'`
convert $png -auto-level -compress none $name".tif"
#rm $png
done
rm *.png
cd ..
compress_name=`echo $font | awk -F "/" '{print $7}' | awk -F "." '{print $1}'`
echo "w we compress the font image file to $compress_name"
tar -cf "$compress_name"".tar.gz" ./tmp_img/* # needn't v
echo "Compress Process was done, Now we will delete the tmp image fold"
rm -rf ./tmp_img
echo "Process done in $font"
done
# -*- coding: utf-8 -*-
#author Zhao KunPeng
import random, string,os
from PIL import Image, ImageDraw,ImageFont
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("font",help="your font path")
args = parser.parse_args()
#def GB2312():
# head = random.randint(0x4E00, 0x9FA5)
# return head
#W, H = (150,100)
W, H = (55,57)
font = ImageFont.truetype(args.font, 55)
if not os.path.exists('./tmp_img'):
os.makedirs('./tmp_img')
for text in range(0x4E00,0x8000): #0x9FA5
c_text=chr(text)
# print(c_text)
img = Image.new('RGB', (W,H),(255,255,255))
draw = ImageDraw.Draw(img)
w, h = draw.textsize(c_text,font=font)
# print( w ,h )
draw.text(((W-w)/2,(H-h)/2), c_text, fill=(0, 0, 0),font=font)
img.save('./tmp_img/'+c_text+".png", "PNG")
#!/usr/bin/env bash
for tiff in $(ls *.tiff)
do
python tiff_parse.py $tiff $tiff.res.txt
done
import random, string
def GB2312():
head = random.randint(0x4E00, 0x9FA5)
return head
s=''
for i in range(1000):
s=s+chr(GB2312())
print(s)
#!/usr/bin/env python
import tifffile as tiff
import numpy as np
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("tiff",help="your tiff file")
parser.add_argument("result",help="your result file")
args = parser.parse_args()
n_name = []
with tiff.TiffFile(args.tiff) as tifile, open(args.result,'w') as res:
images = tifile.asarray()
for page in tifile:
name = page.tags.values()
# print name[-2].name,name[-2].value #document_name name
# print(name[-2].value)
n_name.append(name[-2].value.split('.')[0])
n_name.append('\00')
f = np.array(n_name)
res.write(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment