首页 > 解决方案 > 使用 python 和 PIL 在图像上写德语文本的问题

问题描述

我想读取一个文本文件并从中提取德文文本,然后使用 PIL 和 python 2.7 将其写入 png 图像,但是当使用 .text() 写入图像时,每当 Ü 或一些外来字符出现时,我都会收到未知文本。我使用 arialunicodems.ttf 作为字体。

首先,我使用 Microsoft azure 认知视觉从图像中提取文本,并在每个单词上使用 .encode('utf-8') 并将单词组合成英语句子,然后使用 python 的 mtranslate 库转换为德语。然后我使用 arialunicodems.ttf 作为字体并使用 PIL Image 的 .text() 函数在 png 上绘制文本。它适用于德语、中文、印地语等。但后来我想为用户添加一个功能,以便在翻译不正确的情况下能够更改翻译的文本。为此,我将原始文本和翻译文本保存在一个 .txt 文件中,并将 txt 文件的内容显示给用户,如果需要,用户可以在其中更改它,并且更改的文本再次保存到 txt 文件中。然后使用另一个 python 程序,我在图像中添加了文本。但是,这一次,每当它的 Ü 时,文本就会变得乱七八糟,它在图像上绘制 Ã☐。对于印地语来说,这都是胡言乱语。可能是什么问题呢?

工作代码:我连接单词以造句的部分(保存在可变文本中)。

for word in word_infos:
                bbox = [int(num) for num in word["boundingBox"].split(",")]
                if bbox[0]>=x and bbox[1]>=y and bbox[0]+bbox[2]<=x+w and bbox[1]+bbox[3]<=y+h:
                    text = text+word["text"].encode('utf-8')+" "

我将文本写入图像的部分

im = Image.open("check.png")
d = ImageDraw.Draw(im)
helvetica = ImageFont.truetype("arialunicodems.ttf",10)
d.text((x,y), mtranslate.translate(text, sys.argv[3], sys.argv[2]), font=helvetica, fill=(0,0,0))

不工作的代码:我将提取的文本保存到 txt 文件的部分

for word in word_infos:
                bbox = [int(num) for num in word["boundingBox"].split(",")]
                if bbox[0]>=x and bbox[1]>=y and bbox[0]+bbox[2]<=x+w and bbox[1]+bbox[3]<=y+h:
                    text = text+word["text"].encode('utf-8')+" "
file.write("orignaltext:"+text+"\n")

我从 txt 文件中提取文本并在图像上书写的部分

im = Image.open("check.png")
d = ImageDraw.Draw(im)
file2 = open("1.txt","r")
printframe = file2.readlines()
#j and traceorig is defined to extract text in loop
orig = printframe[j*6+3][traceorig:len(printframe[j*6+3])-1].encode('utf-8')
#xstr,ystr,r,g,b are extracted from image
d.text((int(xstr),int(ystr)), mtranslate.translate(orig,"de","en").encode('utf-8'), font=helvetica, fill=(int(r), int(g), int(b)))

对于英语的“概述”,我想要
德语:
Überblick 在印地语中:अवलोकन
在更新的代码中,当我在终端上打印时,它会正确打印,但在图像上它会写
在德语中:Ã☐berblick
在印地语中:无法找到字符,请查看图片链接印地语翻译的图片

更新1:

生成类似结果的示例代码

#!/usr/bin/python
# -*- coding: utf-8 -*-
from PIL import Image, ImageDraw, ImageFont, ImageFilter
import cv2
import numpy as np
import sys
import os
reload(sys)
sys.setdefaultencoding('utf8')
#file has only one line with text "Überblick"
file1 = open("write.txt","w+")
file1.write("Überblick")
file1.close()
file2 = open("write.txt","r")
content = file2.readlines()
file2.close()
img = np.zeros((300,300,1), np.uint8)
cv2.imwrite("stack.png",img)
im = Image.open("stack.png")
d = ImageDraw.Draw(im)
helvetica = ImageFont.truetype("arialunicodems.ttf",50)
d.text((0,100), content[0].encode('utf-8'), font=helvetica, fill="white")
im.save("processed.png")
os.remove("stack.png")

有关输出,请参见处理的.png。arialunicodems.ttf 文件

标签: imagepython-2.7textunicodepython-imaging-library

解决方案


所以,我自己想通了。任何使用 Python 2.x 和 PIL 在图像上编写 unicode 文本时遇到问题的人,请先阅读此链接。它为不同版本的 python 中的文本编码提供了非常丰富的信息。答案是使用 unicode()。删除 .encode('utf-8') 并使其像:

d.text((0,100), unicode(content[0]), font=helvetica, fill="white")

unicode() 是将任何字符串转换为 unicode 字符串,类似于 str() 转换为字符串。希望这可以帮助有需要的人。


推荐阅读