|
发表于 2020-9-9 09:41:38
|
显示全部楼层
本楼为最佳答案
本帖最后由 kylin121380 于 2020-9-9 21:11 编辑
你这个生成的是txt文件,可以试试用opencv做字符画
- import cv2
- import random
- import numpy as np
- import os
- def img2strimg(frame, K=5):
- if type(frame) != np.ndarray:
- frame = np.array(frame)
- height, width, *_ = frame.shape # 有时返回两个值,有时三个值
- frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- frame_array = np.float32(frame_gray.reshape(-1))
- # 设置相关参数。
- criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
- flags = cv2.KMEANS_RANDOM_CENTERS
- compactness, labels, centroids = cv2.kmeans(frame_array, K, None, criteria, 10, flags)
- centroids = np.uint8(centroids)
- centroids = centroids.flatten()
- centroids_sorted = sorted(centroids)
- centroids_index = np.array([centroids_sorted.index(value) for value in centroids])
- bright = [abs((3 * i - 2 * K) / (3 * K)) for i in range(1, 1 + K)]
- bright_bound = bright.index(np.min(bright))
- shadow = [abs((3 * i - K) / (3 * K)) for i in range(1, 1 + K)]
- shadow_bound = shadow.index(np.min(shadow))
- labels = labels.flatten()
- labels = centroids_index[labels]
- labels_picked = [labels[rows * width:(rows + 1) * width:2] for rows in range(0, height, 2)]
- canvas = np.zeros((3 * height, 3 * width, 3), np.uint8)
- canvas.fill(255)
- y = 8
- for rows in labels_picked:
- x = 0
- for cols in rows:
- if cols <= shadow_bound:
- cv2.putText(canvas, str(random.randint(2, 9)),
- (x, y), cv2.FONT_HERSHEY_PLAIN, 0.45, 1)
- elif cols <= bright_bound:
- cv2.putText(canvas, "-", (x, y),
- cv2.FONT_HERSHEY_PLAIN, 0.4, 0, 1)
- x += 6
- y += 6
- return canvas
- if __name__ == '__main__':
- imgdir = '(55).jpg'
- img = cv2.imread(imgdir)
- str_img = img2strimg(img)
- cv2.imwrite('1.jpg', str_img)
复制代码 |
|