Skip to content

Commit

Permalink
ttf-to-jpg 기능구현 완료
Browse files Browse the repository at this point in the history
—
Redmine Issue Progress / ID / Time :  80% #35 @1h
—
What :
- ttf-to-jpg 기능구현
- README.md 작성
- 테스트 미실시

Why :
- 학습에 jpg 이미지 필요
—
*Signed-off-by: Junyoung, Sung <[email protected]>*
  • Loading branch information
sungjunyoung committed Oct 9, 2017
1 parent d2a5451 commit 6b96f44
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
node_modules/
.idea/
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
# ttf-to-jpg

> node implement that makes .ttf files to .jpg set
> python implement that makes .ttf files to .jpg set **JUST KOREAN**
## requirement
- Python 3.6
- Pillow 4.2.1

```
pip install -r requirements.txt
```

## usage
> there are two example fonts (.ttf) in ttf_dir
```
ttf-to-jpg.py [-h] [--ttf_dir TTF_DIR] [--jpg_dir JPG_DIR]
[--size SIZE] [--offset OFFSET]
```

#### help
```
python ttf-to-jpg.py --help
```

#### arguments
- `ttf_dir` : directory that includes .ttf files
- `ttf_dir` : directory that includes .ttf files
- `jpg_dir` : directory that includes results of .jpg
- `size` : size of result jpg
- `offset` : offset (padding)



## Thanks to :)
- code reference : [zi2zi/font2img.py](https://github.com/kaonashi-tyc/zi2zi/blob/master/font2img.py) by kaonashi-tyc
- example fonts : [우아한 형제들, 배달의민족 무료 폰트(도현체, 주아체)](http://font.woowahan.com/) by 우아한 형제들
1 change: 1 addition & 0 deletions charset.json

Large diffs are not rendered by default.

22 changes: 0 additions & 22 deletions package.json

This file was deleted.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pillow==4.2.1
12 changes: 0 additions & 12 deletions ttf-to-jpg.js

This file was deleted.

62 changes: 62 additions & 0 deletions ttf-to-jpg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import argparse
import os
import json
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont


def parse_args():
desc = "ttf fonts to jpg images set (JUST KOREAN)"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('--ttf_dir', type=str, default='ttf_dir',
help='directory that includes .ttf files', required=False)
parser.add_argument('--jpg_dir', type=str, default='jpg_dir',
help='directory that includes results of .jpg', required=False)
parser.add_argument('--size', type=int, default=256,
help='size of result jpg', required=False)
parser.add_argument('--offset', type=int, default=20,
help='offset', required=False)
return parser.parse_args()


def draw_char(font, char, size, offset):
img = Image.new("RGB", (size, size), (255, 255, 255))
draw = ImageDraw.Draw(img)
draw.text((offset, offset), char, (0, 0, 0), font=font)
return img


def main():
args = parse_args()
ttf_dir = args.ttf_dir
jpg_dir = args.jpg_dir
charset = json.load(open('./charset.json'))['kr']

char_size = args.size - (args.offset * 2)

# if jpg_dir not exist
if not os.path.exists(jpg_dir):
os.makedirs(jpg_dir)


for _, _, ttfs in os.walk(ttf_dir):
for ttf in ttfs:
font = ImageFont.truetype(ttf_dir + '/' + ttf, size=char_size)
count = 0
for c in charset:
if count % 100 == 0:
print("WORKING:: %s - character %c" % (ttf, c))

if not os.path.exists(jpg_dir + '/' + c):
os.makedirs(jpg_dir + '/' + c)
img = draw_char(font, c, args.size, args.offset)
if img:
img.save(os.path.join(jpg_dir + '/' + c,
"%s_%s.jpg" % (c, ttf.split('.')[0])))
count += 1

print("!! COMPLETE:: %s" % (ttf))

if __name__ == '__main__':
main()
Binary file added ttf_dir/BMDOHYEON_ttf.ttf
Binary file not shown.
Binary file added ttf_dir/BMJUA_ttf.ttf
Binary file not shown.

0 comments on commit 6b96f44

Please sign in to comment.