简体   繁体   English

使用Unicode字符将文本转换为图像

[英]Converting Text to Image with Unicode characters

So, the idea here is that the given text, which happens to be Devanagari character such as संस्थानका कर्मचारी and I want to convert given text to image. 因此,这里的想法是给定的文本恰好是梵文字符,例如संस्थानका कर्मचारी ,我想将给定的文本转换为图像。 Here is what I have attempted. 这是我尝试过的。

def draw_image(myString):

    width=500
    height=100
    back_ground_color=(255,255,255)
    font_size=10
    font_color=(0,0,0)
    unicode_text = myString

    im  =  Image.new ( "RGB", (width,height), back_ground_color )
    draw  =  ImageDraw.Draw (im)
    unicode_font = ImageFont.truetype("arial.ttf", font_size)
    draw.text ( (10,10), unicode_text, font=unicode_font, fill=font_color )
    im.save("text.jpg")

    if cv2.waitKey(0)==ord('q'):
        cv2.destroyAllWindows()

But the font is not recognized, so the image consists of boxes, and other characters that are not understandable. 但是无法识别字体,因此图像由框和其他无法理解的字符组成。 So, which font should I use to get the correct image? 那么,我应该使用哪种字体来获取正确的图像? Or is there any better approach to convert, the given text in character such as those, to image? 还是有更好的方法将给定字符(如字符)中的文本转换为图像?

So I had a similar problem when I wanted to write text in Urdu onto images, firstly you need the correct font since writing purely with PIL or even openCV requires the appropriate Unicode characters, and even when you get the appropriate font the letters of one word are disjointed, and you don't get the correct results. 因此,当我想用​​乌尔都语在图像上书写文本时,我遇到了类似的问题,首先,您需要正确的字体,因为仅使用PIL甚至openCV进行书写都需要适当的Unicode字符,甚至当您获得适当的字体时,一个单词的字母也是如此脱节,您将无法获得正确的结果。

To resolve this you have to stray a bit from the traditional python-only approach since I was creating artificial datasets for an OCR, i needed to print large sets of such words onto a white background. 为了解决这个问题,由于我正在为OCR创建人工数据集,因此您不得不偏离传统的仅使用python的方法,我需要将大量此类单词打印在白色背景上。 I decided to use graphics software for this. 我决定为此使用图形软件。 Since some like photoshop even allows you to write scripts to automate processes. 由于像photoshop这样的工具甚至允许您编写脚本来自动执行过程。

The software I went for was GIMP, which allows you to quickly write and run extensions.scripts to automate the process. 我购买的软件是GIMP,它使您可以快速编写和运行extensions.scripts以自动执行该过程。 It allows you to write an extension in python, or more accurately a modified version of python, known as python-fu. 它允许您使用python编写扩展,或更准确地说是python的修改版本,称为python-fu。 Documentation was limited so it was difficult to get started, but with some persistence, I was able to write functions that would read text from a text file, and place them on white backgrounds and save to disk. 文档是有限的,所以很难上手,但是在一些持久性的支持下,我能够编写一些功能,可以从文本文件中读取文本,并将其放置在白色背景上并保存到磁盘上。

I was able to generate around 300k images from this in a matter of hours. 我可以在几个小时内从中生成约30万张图像。 I would suggest if you too are aiming for large amounts of text writing then you too rely on python-fu and GIMP. 我建议如果您也打算大量编写文本,那么您也应该依赖python-fu和GIMP。

For more info you may refer to the GIMP Python Documentation 有关更多信息,请参阅GIMP Python文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM