Sometimes, we need to convert text to images, such as hair-length tweets, or don't want people to easily copy our text content. There are a lot of similar tools at the moment, but I think it is not very easy to use, so I try to achieve a.
In Python, the PIL (Python Imaging library) is the most common drawing library, and naturally, try to start with PIL.
Convert text to a picture using PIL
Say the conversion is not really appropriate, the real process is: first in memory to generate a picture, the required text is drawn to this picture, and then save the picture to the specified location. The code is as follows:
#-*-coding:utf-8-*-import osimport Image, Imagefont, Imagedraw text = u "This is a test text, testing 123. "Im = Image.new (" RGB ", (+), (255, 255, 255)) Dr = Imagedraw.draw (im) font = Imagefont.truetype (Os.path.join (" Fonts ", "Msyh.ttf") Dr.text ((5), text, Font=font, fill= "#000000") Im.show () im.save ("T.png")
The resulting picture is as follows:
Cup has happened, Chinese characters are not displayed properly!
Search the Internet a lap, and found that it seems to be a bug,pil in the current version of PIL, the rendering of non-ASCII character bitmap fonts cannot be handled correctly. For fonts such as Arial, only >= 18px will be treated as a vector font, that is, only if the font >= 18px, the text can be displayed normally:
Font = Imagefont.truetype (Os.path.join ("Fonts", "SIMSUN.TTC"), 18)
The effect is as follows:
Increase the font Although the Chinese characters can not be displayed correctly, but still did not solve our original intention: the use of bitmap font for rendering. However, the goal of using the PIL at this stage seems a bit difficult to achieve.
Render bitmap fonts using PyGame
Python's third-party modules or components can be used to draw in addition to PIL, there are Pycairo, matplotlib, PyGame and so on. Here, I use PyGame to complete the rendering of bitmap fonts.
The code is as follows:
#-*-coding:utf-8-*-import osimport pygame pygame.init () Text = U "This is a test text, testing 123. "Font = Pygame.font.Font (Os.path.join (" Fonts "," SIMSUN.TTC "), Rtext = Font.render (text, True, (0, 0, 0), (255, 255, 255 )) Pygame.image.save (Rtext, "t.jpg")
The effect is as follows:
You can see that using pyGame, the problem of lattice fonts has finally been fixed.
Combined with PIL and pyGame
Although pyGame can solve the problem of bitmap font rendering, it is more powerful to deal with the image or PIL. So why don't we combine the two together? Use PyGame to render the bitmap font, and then use PIL to generate the entire picture.
The code is as follows:
#-*-coding:utf-8-*-import osimport stringioimport Image, Imagefont, Imagedrawimport pygame pygame.init () Text = U "This is a Section Test text, testing 123. "Im = Image.new (" RGB ", (+), (255, 255, 255)) #dr = Imagedraw.draw (im) #font = Imagefont.truetype (Os.path.join (" Fonts " , "SIMSUN.TTC"), Font = Pygame.font.Font (Os.path.join ("Fonts", "SIMSUN.TTC"), #dr. Text ((5), text, Font=font, F Ill= "#000000") Rtext = Font.render (text, True, (0, 0, 0), (255, 255, 255)) #pygame. Image.Save (Rtext, "t.gif") Sio = Stringio . Stringio () Pygame.image.save (Rtext, Sio) sio.seek (0) line = Image.open (SIO) Im.paste (line, (5)) Im.show () Im.save (" T.png ")
The principle is very simple, first the text is rendered as a picture with pyGame, the rendering result is saved in a Stringio object, and then loaded with PIL. The advantage of using Stringio is that everything is done in memory, and it is not necessary to save it to the hard disk and then read it with PIL because the efficiency of the hard disk IO is relatively low.
The final effect is as follows:
Here, the ability to use Python to convert text to pictures is basically implemented, using PIL and PyGame.
Of course, the above code also only solves the most basic problem, a really usable text to picture tool, but also should solve the following problems: Long text line-breaking problem, English word hyphenation problem, punctuation, such as the line of words. The analysis of these issues is not short, and this time it is skipped. The following is a comprehensive consideration of a number of factors after the creation of the "Moonlight Lotus Pond":