import cv2
import matplotlib.pyplot as plt
import numpy as np
# 154,205
# image_raw = Image.open("text_img/tt.jpg")
img = cv2.imread('text_img/tt3.jpg',0)
black = cv2.imread('text_img/black.png',0)
word = cv2.imread('text_img/word2.png',0)
# 图片二值化
ret, mask_all = cv2.threshold(src=img, # 要二值化的图片
thresh=120, # 全局阈值
maxval=255, # 大于全局阈值后设定的值
type=cv2.THRESH_BINARY)# 设定的二值化类型,
arr = []
# 拼接汉字图片
for x in mask_all:
result = black.copy()
for y in x:
print (y)
if y==0:
result = np.hstack((result,word))
else:
result = np.hstack((result, black))
arr.append(result)
result = arr[0]
for i in range (1,len(arr)):
result = np.vstack((result, arr[i]))
# 保存拼接的图片
cv2.imwrite("text_img/result.jpg",result)
Python OpenCV 用汉字生成图片
最新推荐文章于 2024-07-10 23:54:36 发布