-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Labels
Description
What did you do?
1. Create a script file char.py for drawing.
#!/usr/bin/env python3
"""Draw a text image from a font
"""
import sys
import os
import argparse
from PIL import Image, ImageDraw, ImageFont
def color_hex_to_tuple(color):
"""Convert hex string RRGGBBAA to (R, G, B, A)
"""
return (int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16), int(color[6:8], 16))
def draw_text(text, font, size=40, fgcolor='000000FF', bgcolor='FFFFFF00', output='text.png'):
if not output:
return
if type(size) == str:
size = int(size)
if type(fgcolor) == str:
fgcolor = color_hex_to_tuple(fgcolor)
if type(bgcolor) == str:
bgcolor = color_hex_to_tuple(bgcolor)
img = Image.new('RGBA', (size, size), bgcolor)
draw = ImageDraw.Draw(img)
fnt = ImageFont.truetype(font, size)
dx, dy = fnt.getoffset(text)
draw.text((-dx, -dy), text, font=fnt, fill=fgcolor)
img.save(output)
def main():
root = os.path.dirname(__file__)
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__,
)
parser.add_argument('text',
help="""The text to draw.""")
parser.add_argument('font',
help="""The font used to draw the text.""")
parser.add_argument('--size', default=40,
help="""Image size in pixels. Default: %(default)s""")
parser.add_argument('--fgcolor', default='000000FF',
help="""Foreground color in hex RRGGBBAA. Default: %(default)s""")
parser.add_argument('--bgcolor', default='FFFFFF00',
help="""Background color in hex RRGGBBAA. Default: %(default)s""")
parser.add_argument('--output', default=os.path.join(root, 'text.png'),
help="""Output file path. Default: %(default)s""")
args = vars(parser.parse_args())
draw_text(**args)
if __name__ == "__main__":
main()2. Download http://fonts.jp/hanazono/ font and put in the same directory.
3. Try several ways to draw the text image:
3.1. Run below command on Windows:
chcp 950
char.py "人" HanaMinA.ttf
3.2. Run below command on Windows:
chcp 65001
char.py "人" HanaMinA.ttf
3.3. Run below command on Windows:
chcp 950
char.py "𠀀" HanaMinB.ttf
3.4. Run below command on Windows:
chcp 65001
char.py "𠀀" HanaMinB.ttf
3.5. Run below command on Linux:
char.py "𠀀" HanaMinB.ttf
What did you expect to happen?
3.* should all draw the image successfully.
What actually happened?
3.3 and 3.4 doesn't draw the image as expected.
What versions of Pillow and Python are you using?
PIL.PILLOW_VERSION = 5.2.0
Tested on Windows 7, SP1 and Linux Ubuntu 16.
Reactions are currently unavailable


