Created
August 3, 2012 15:22
-
-
Save deeplook/3248606 to your computer and use it in GitHub Desktop.
A script for comparing TrueType font rendering on different platforms.
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""A script for comparing TrueType font rendering on different platforms. | |
""" | |
import os | |
import sys | |
import platform | |
import argparse | |
from hashlib import md5 | |
from cStringIO import StringIO | |
from os.path import basename, splitext | |
from PIL import Image, ImageFont | |
from PIL.ImageDraw import Draw | |
try: | |
import png | |
HAVE_PYPNG = True | |
except ImportError: | |
HAVE_PYPNG = False | |
__version__ = '0.2.1' | |
__license__ = 'GPL v3' | |
__date__ = '2012-08-02' | |
__author__ = 'Dinu Gherman' | |
def create_simple_pil(text, font_path, font_size, format="PNG"): | |
"Create and return a bitmap containing a string using PIL." | |
temp = StringIO() | |
im = Image.new("RGB", (400, 100)) | |
draw = Draw(im) | |
if text: | |
print "Font", font_path | |
print "Text", text | |
print "Text repr", repr(text) | |
font = ImageFont.truetype(font_path, font_size) | |
fsize = font.getsize(text) | |
print "PIL font getsize", fsize | |
x, y = 0, 0 | |
draw.text((x, y), text, fill="red", font=font) | |
draw.rectangle(((x, y), (x + fsize[0], y + fsize[1])), outline="green") | |
im.save(temp, format=format) | |
temp.seek(0) | |
data = temp.read() | |
return data | |
def _main(): | |
msg = "Testing font rendering generated with PIL on various platforms." | |
parser = argparse.ArgumentParser(description=msg) | |
paa = parser.add_argument | |
paa("-v", "--version", action='version', version='%(prog)s ' + __version__, | |
help="Show program's version number and exit.") | |
paa("-t", "--text", dest="text", metavar="TEXT", default='FOO BAR', | |
help="Text to be displayed (default: FOO BAR).") | |
paa("-f", "--format", dest="format", metavar="FORMAT", default='PNG', | |
help="PIL bitmap format to be used (default: PNG).") | |
paa("-s", "--font-size", metavar='FONTSIZE', dest="font_size", default='48', | |
help="Font size to be used (default: 48).") | |
paa(metavar='FONTPATH', dest="font_path", nargs='+', | |
help="Path(s) of the TTF font file(s) to be used.") | |
options = parser.parse_args() | |
if options.text and options.font_path: | |
print "Platform", platform.platform() | |
print "Python", platform.python_version(), sys.executable | |
print "PIL Image version", Image.VERSION | |
print "PIL Image file", Image.__file__ | |
for font_path in options.font_path: | |
text = options.text.decode("utf8") | |
font_size = int(options.font_size) | |
format = options.format | |
file_data = create_simple_pil(text, font_path, font_size, format=format) | |
print "%s size %d" % (format.upper(), len(file_data)) | |
fontname = splitext(basename(font_path))[0] | |
path = "test_%s_%s.%s" % (fontname, platform.uname()[0], format.lower()) | |
open(path, "wb").write(file_data) | |
if format.upper() == "PNG": | |
f = file=open(path, "rb") | |
if HAVE_PYPNG: | |
r = png.Reader(file=f) | |
img_data = list(r.read()[2]) | |
print "PNG data MD5 hash", md5(str(img_data)).hexdigest() | |
else: | |
img_data = f.read() | |
print "PNG MD5 hash", md5(str(img_data)).hexdigest() | |
print "*Warning: PyPNG not found" | |
else: | |
img_data = open(path).read() | |
print "%s MD5 hash %s" % (format.upper(), md5(str(img_data)).hexdigest()) | |
print "Output", path | |
if __name__ == "__main__": | |
_main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment