Skip to content

Commit

Permalink
opencv图片添加中文文档和代码提交
Browse files Browse the repository at this point in the history
  • Loading branch information
vipstone committed May 6, 2018
1 parent fcd6d04 commit 6713233
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

[OpenCV环境搭建](doc/settingup.md)

[OpenCV图片添加中文](doc/chinese.md)

[Tesseract OCR文字识别](doc/tesseractOCR.md)

[图片人脸检测(OpenCV版)](doc/detectionOpenCV.md)
Expand Down
70 changes: 70 additions & 0 deletions doc/chinese.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# OpenCV图片添加中文 #
OpenCV添加文字的方法putText(...),添加英文是没有问题的,但如果你要添加中文就会出现“???”的乱码,需要特殊处理一下。

下文提供封装好的(代码)方法,供OpenCV添加中文使用。

# 效果预览 #

<img src="https://raw.githubusercontent.com/vipstone/faceai/master/res/chinese.png" width = "300" height = "200" alt="效果展示" />

# 实现思路 #
使用PIL的图片绘制添加中文,可以指定字体文件,那么也就是说使用PIL可以实现中文的输出。

有思路之后,接下来的工作就简单了。

1. OpenCV图片格式转换成PIL的图片格式;
1. 使用PIL绘制文字;
1. PIL图片格式转换成OpenCV的图片格式;

# 代码分解 #

**OpenCV图片转换为PIL图片格式**

```
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
```

**使用PIL绘制文字**
```
draw = ImageDraw.Draw(img)
fontText = ImageFont.truetype("font/simsun.ttc", textSize, encoding="utf-8")
draw.text((left, top), "文字内容", textColor, font=fontText)
```
字体文件为:simsun.ttc,Windows可以在C:\Windows\Fonts下面查找。



**PIL图片格式转换成OpenCV的图片格式**
```
cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)
```


# 完整代码 #

封装好的完整方法

```
#coding=utf-8
#中文乱码处理
import cv2
import numpy
from PIL import Image, ImageDraw, ImageFont
def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
if (isinstance(img, numpy.ndarray)): #判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fontText = ImageFont.truetype(
"font/simsun.ttc", textSize, encoding="utf-8")
draw.text((left, top), text, textColor, font=fontText)
return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)
```

代码调用

```
img = cv2ImgAddText(img, "大家好,我是星爷", 140, 60, (255, 255, 0), 20)
```
25 changes: 25 additions & 0 deletions faceai/ChineseText.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#coding=utf-8
#中文乱码处理

import cv2
import numpy
from PIL import Image, ImageDraw, ImageFont

img = cv2.imread("img/xingye-1.png")


def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
if (isinstance(img, numpy.ndarray)): #判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fontText = ImageFont.truetype(
"font/simsun.ttc", textSize, encoding="utf-8")
draw.text((left, top), text, textColor, font=fontText)
return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)


img = cv2ImgAddText(img, "大家好,我是星爷", 140, 60, (255, 255, 0), 20)

cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Binary file added faceai/font/simsun.ttc
Binary file not shown.

0 comments on commit 6713233

Please sign in to comment.