Skip to content

Instantly share code, notes, and snippets.

@destan
Last active January 10, 2024 06:32

Revisions

  1. destan revised this gist May 8, 2013. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions text2png.py
    Original file line number Diff line number Diff line change
    @@ -5,13 +5,13 @@
    from PIL import Image
    from PIL import ImageDraw

    def text2png(text, fullpath, color = "#000", bgcolor = "#FFF", fontfullpath = None, fontsize = 25, leftpadding = 3, rightpadding = 3, width = 200):
    def text2png(text, fullpath, color = "#000", bgcolor = "#FFF", fontfullpath = None, fontsize = 13, leftpadding = 3, rightpadding = 3, width = 200):
    REPLACEMENT_CHARACTER = u'\uFFFD'
    NEWLINE_REPLACEMENT_STRING = ' ' + REPLACEMENT_CHARACTER + ' '

    #prepare linkback
    linkback = "http://github.com/dedeler"
    fontlinkback = ImageFont.load_default()
    linkback = "created via http://ourdomain.com"
    fontlinkback = ImageFont.truetype('font.ttf', 8)
    linkbackx = fontlinkback.getsize(linkback)[0]
    linkback_height = fontlinkback.getsize(linkback)[1]
    #end of linkback
    @@ -52,7 +52,7 @@ def text2png(text, fullpath, color = "#000", bgcolor = "#FFF", fontfullpath = No
    y += line_height

    # add linkback at the bottom
    draw.text( (width - linkbackx - rightpadding, img_height - linkback_height), linkback, color, font=fontlinkback)
    draw.text( (width - linkbackx, img_height - linkback_height), linkback, color, font=fontlinkback)

    img.save(fullpath)

  2. destan created this gist May 8, 2013.
    60 changes: 60 additions & 0 deletions text2png.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    # coding=utf8

    import PIL
    from PIL import ImageFont
    from PIL import Image
    from PIL import ImageDraw

    def text2png(text, fullpath, color = "#000", bgcolor = "#FFF", fontfullpath = None, fontsize = 25, leftpadding = 3, rightpadding = 3, width = 200):
    REPLACEMENT_CHARACTER = u'\uFFFD'
    NEWLINE_REPLACEMENT_STRING = ' ' + REPLACEMENT_CHARACTER + ' '

    #prepare linkback
    linkback = "http://github.com/dedeler"
    fontlinkback = ImageFont.load_default()
    linkbackx = fontlinkback.getsize(linkback)[0]
    linkback_height = fontlinkback.getsize(linkback)[1]
    #end of linkback

    font = ImageFont.load_default() if fontfullpath == None else ImageFont.truetype(fontfullpath, fontsize)
    text = text.replace('\n', NEWLINE_REPLACEMENT_STRING)

    lines = []
    line = u""

    for word in text.split():
    print word
    if word == REPLACEMENT_CHARACTER: #give a blank line
    lines.append( line[1:] ) #slice the white space in the begining of the line
    line = u""
    lines.append( u"" ) #the blank line
    elif font.getsize( line + ' ' + word )[0] <= (width - rightpadding - leftpadding):
    line += ' ' + word
    else: #start a new line
    lines.append( line[1:] ) #slice the white space in the begining of the line
    line = u""

    #TODO: handle too long words at this point
    line += ' ' + word #for now, assume no word alone can exceed the line width

    if len(line) != 0:
    lines.append( line[1:] ) #add the last line

    line_height = font.getsize(text)[1]
    img_height = line_height * (len(lines) + 1)

    img = Image.new("RGBA", (width, img_height), bgcolor)
    draw = ImageDraw.Draw(img)

    y = 0
    for line in lines:
    draw.text( (leftpadding, y), line, color, font=font)
    y += line_height

    # add linkback at the bottom
    draw.text( (width - linkbackx - rightpadding, img_height - linkback_height), linkback, color, font=fontlinkback)

    img.save(fullpath)

    #show time
    text2png(u"This is\na\ntest şğıöç zaa xd ve lorem hipster", 'test.png', fontfullpath = "font.ttf")