Use a Python script to convert text to image sharing instances,
Sometimes we need to convert text into images, such as long Weibo posts, or do not want people to easily copy our text content. At present, there are already a lot of similar tools, but I don't think they are very easy to use, so I tried to implement one myself.
In Python, PIL (Python Imaging Library) is the most commonly used drawing Library. Naturally, it tries to start with PIL.
Use PIL to convert text to image
The conversion is not appropriate. The actual process is: first generate an image in the memory, draw the required text to the image, and then save the image to the specified position. The Code is as follows:
#-*-Coding: UTF-8-*-import osimport Image, ImageFont, ImageDraw text = u "is a test text, test 123. "Im = Image. new ("RGB", (300, 50), (255,255,255) dr = ImageDraw. draw (im) font = ImageFont. truetype (OS. path. join ("fonts", "msyh. ttf "), 14) dr. text (10, 5), text, font = font, fill = "#000000") im. show () im. save ("t.png ")
The generated image is as follows:
The cup has occurred, and Chinese characters are not properly displayed!
After searching for a circle on the internet, it seems to be a bug in PIL. In the current version of PIL, rendering of lattice fonts with non-ASCII characters cannot be properly processed. For fonts like, the font is treated as a vector font only when the font is greater than or equal to 18px. That is to say, the text can be normally displayed only when the font is greater than or equal to 18px:
font = ImageFont.truetype(os.path.join("fonts", "simsun.ttc"), 18)
The effect is as follows:
Although increasing the font size solves the problem that Chinese characters cannot be displayed normally, it still does not solve our initial intention: to use dot matrix fonts for rendering. However, it seems a bit difficult to achieve this goal using PIL at the current stage.
Use pyGame to render dot matrix Fonts
Python has many third-party modules or components. In addition to PIL, Python also includes Pycairo, matplotlib, and pyGame. Here, I use pyGame to complete the rendering of dot matrix fonts.
The Code is as follows:
#-*-Coding: UTF-8-*-import osimport pygame. init () text = u "this is a test text, test 123. "Font = pygame. font. font (OS. path. join ("fonts", "simsun. ttc "), 14) rtext = font. render (text, True, (0, 0, 0), (255,255,255) pygame. image. save (rtext, "t.jpg ")
The effect is as follows:
We can see that the problem of dot matrix fonts is finally solved by using pyGame.
Combined with PIL and pyGame
Although pyGame can solve the problem of dot matrix font rendering, PIL is more powerful when it comes to image processing. So why don't we combine the two? Use pyGame to render the dot matrix font and then use PIL to generate the entire image.
The Code is as follows:
#-*-Coding: UTF-8-*-import osimport StringIOimport Image, ImageFont, ImageDrawimport pygame. init () text = u "is a test text, test 123. "Im = Image. new ("RGB", (300, 50), (255,255,255) # dr = ImageDraw. draw (im) # font = ImageFont. truetype (OS. path. join ("fonts", "simsun. ttc "), 18) font = pygame. font. font (OS. path. join ("fonts", "simsun. ttc "), 14) # dr. text (10, 5), text, font = font, fill = "#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, SiO2) sio. seek (0) line = Image. open (SiO2) im. paste (line, (10, 5) im. show () im. save ("t.png ")
The principle is very simple. First, use pyGame to render the text as an image, save the rendering result in a StringIO object, and then load it with PIL. The advantage of using StringIO is that all operations are performed in the memory, and you do not need to save it to the hard disk and then use PIL to read it, because the hard disk IO efficiency is relatively low.
The final result is as follows:
Here, the function of converting text into images using Python is basically implemented, and PIL and pyGame are used.
Of course, the above Code only solves the most basic problems. A truly usable text-to-image conversion tool should also solve the following problems: long text line breaks, English Word breaks, punctuation line breaks, and so on. The analysis of these problems is not too short. This time, I skipped it. The following is a comprehensive consideration of the many factors generated by the lotus pond moonlight: