forked from sungjunyoung/ttf-to-jpg
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
— 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
1 parent
d2a5451
commit 6b96f44
Showing
9 changed files
with
98 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
node_modules/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 우아한 형제들 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Pillow==4.2.1 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.