Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change for chinese #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added chinese.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 27 additions & 21 deletions render.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import tempfile

def render(points, filename, width=3000, height=1800, fontfile=DEFAULT_FONT, fontsize=12, margin=0.05, transparency=0.5):
def render(points, filename, width=3000, height=1800, fontfile=DEFAULT_FONT, fontsize=20, margin=0.05, transparency=0.5):
"""
Render t-SNE text points to an image file.
points is a list of tuples of the form (title, x, y).
Expand All @@ -25,14 +25,16 @@ def render(points, filename, width=3000, height=1800, fontfile=DEFAULT_FONT, fon
H = height

#im = Image.new("L", (W, H), 255)
im = Image.new("RGBA", (W, H), (0,0,0))
#im = Image.new("RGBA", (W, H), (0,0,0))
im = Image.new("RGB", (W, H), (255,255,255))
font = ImageFont.truetype("/usr/share/fonts/winFonts/simsun.ttc", 24)

# use a bitmap font
#font = ImageFont.load("/usr/share/fonts/liberation/LiberationSans-Italic.ttf")

if fontfile is not None:
assert os.path.exists(fontfile)
font = ImageFont.truetype(fontfile, fontsize)
#if fontfile is not None:
# assert os.path.exists(fontfile)
# font = ImageFont.truetype(fontfile, fontsize)

#draw = ImageDraw.Draw(im)
#draw.text((10, 10), "hello", font=font)
Expand All @@ -57,10 +59,11 @@ def render(points, filename, width=3000, height=1800, fontfile=DEFAULT_FONT, fon
maxy += dy * margin


alpha = Image.new("L", im.size, "black")
#alpha = Image.new("L", im.size, "black")

for (idx, pt) in enumerate(points):
(title, x, y) = pt
title = title.decode('utf-8')
# print x, minx
# print 1. * (x - minx) / (maxx - minx)
# print y, miny
Expand All @@ -71,20 +74,22 @@ def render(points, filename, width=3000, height=1800, fontfile=DEFAULT_FONT, fon

# Make a grayscale image of the font, white on black.
pos = (x, y)
imtext = Image.new("L", im.size, 0)
drtext = ImageDraw.Draw(imtext)
#imtext = Image.new("L", im.size, 0)
#drtext = ImageDraw.Draw(imtext)
drtext = ImageDraw.Draw(im)
print >> sys.stderr, "Rendering title (#%d):" % idx, repr(title)
if fontfile is not None:
drtext.text(pos, title, font=font, fill=(256-256*transparency))
else:
drtext.text(pos, title, fill=(256-256*transparency))
#if fontfile is not None:
#drtext.text(pos, title, font=font, fill=(256-256*transparency))
#else:
#drtext.text(pos, title, fill=(256-256*transparency))
# drtext.text(pos, title, font=font, fill=128)

drtext.ink = 0 + 0 * 256 + 0 * 256 * 256 #black
drtext.text(pos, title, font=font)
# Add the white text to our collected alpha channel. Gray pixels around
# the edge of the text will eventually become partially transparent
# pixels in the alpha channel.
# alpha = ImageChops.lighter(alpha, imtext)
alpha = ImageChops.add(alpha, imtext)
# alpha = ImageChops.add(alpha, imtext)

# Make a solid color, and add it to the color layer on every pixel
# that has even a little bit of alpha showing.
Expand All @@ -98,14 +103,15 @@ def render(points, filename, width=3000, height=1800, fontfile=DEFAULT_FONT, fon
# break

# Add the alpha channel to the image, and save it out.
im.putalpha(alpha)
#im.putalpha(alpha)

tmpf = tempfile.NamedTemporaryFile(suffix=".png")
#tmpf = tempfile.NamedTemporaryFile(suffix=".png")

#im.save("transtext.png", "PNG")
print >> sys.stderr, "Rendering alpha image to file", tmpf.name
im.save(tmpf.name)
#print >> sys.stderr, "Rendering alpha image to file", tmpf.name
#im.save(tmpf.name)
im.save('chinese.png')

cmd = "convert %s -background white -flatten %s" % (tmpf.name, filename)
print >> sys.stderr, "Flattening image", tmpf.name, "to", filename, "using command:", cmd
os.system(cmd)
#cmd = "convert %s -background white -flatten %s" % (tmpf.name, filename)
#print >> sys.stderr, "Flattening image", tmpf.name, "to", filename, "using command:", cmd
#os.system(cmd)
3 changes: 2 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env python

import string, numpy, gzip
o = gzip.open("testdata/english-embeddings.turian.txt.gz", "rb")
#o = gzip.open("testdata/english-embeddings.turian.txt.gz", "rb")
o = open('testdata/plot_phrase.vector', 'rb')
titles, x = [], []
for l in o:
toks = string.split(l)
Expand Down