使用Unicode字符将文本转换为图像 [英] Converting Text to Image with Unicode characters

查看:118
本文介绍了使用Unicode字符将文本转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这里的想法是给定的文本,恰好是梵文字符,例如संस्थानकाकर्मचारी,我想将给定的文本转换为图像.这是我尝试过的.

  def draw_image(myString):宽度= 500高度= 100back_ground_color =(255,255,255)font_size = 10font_color =(0,0,0)unicode_text = myStringim = Image.new("RGB",(width,height),back_ground_color)画= 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")如果cv2.waitKey(0)== ord('q'):cv2.destroyAllWindows() 

但是无法识别字体,因此图像由方框和其他无法理解的字符组成.那么,我应该使用哪种字体来获取正确的图像?还是有更好的方法将给定字符(如字符)中的文本转换为图像?

解决方案

所以当我想在Urdu中将文本写到图像上时,我遇到了类似的问题,首先,您需要使用正确的字体,因为仅使用PIL甚至openCV进行书写都需要适当的Unicode字符,甚至当您获得适当的字体时,一个单词的字母也会脱节,并且您不会得到正确的结果.

要解决此问题,您必须偏离传统的仅使用python的方法,因为我正在为OCR创建人工数据集,因此我需要将大量此类单词打印在白色背景上.我决定为此使用图形软件.由于像photoshop这样的工具甚至允许您编写脚本来自动执行流程.

我追求的软件是GIMP,它使您可以快速编写和运行extensions.scripts以自动执行该过程.它允许您用python编写扩展,或更准确地说是python的修改版本,称为python-fu.文档是有限的,所以很难上手,但是在一些持久性的支持下,我能够编写一些功能,可以从文本文件中读取文本,并将其放置在白色背景上并保存到磁盘.

我可以在几个小时内从中生成约30万张图像.我建议如果您也打算大量编写文本,那么您也应该依赖python-fu和GIMP.

有关更多信息,您可以参考 GIMP Python文档

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.

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. I decided to use graphics software for this. Since some like photoshop even allows you to write scripts to automate processes.

The software I went for was GIMP, which allows you to quickly write and run extensions.scripts to automate the process. It allows you to write an extension in python, or more accurately a modified version of python, known as 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. I would suggest if you too are aiming for large amounts of text writing then you too rely on python-fu and GIMP.

For more info you may refer to the GIMP Python Documentation

这篇关于使用Unicode字符将文本转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆