Python特效之数字成像方法详解

作者:autofelix 时间:2022-09-12 13:16:22 

一、特效预览

Python特效之数字成像方法详解

处理前

Python特效之数字成像方法详解

处理后

Python特效之数字成像方法详解

细节放大后

二、程序原理

1.将图片转为灰白图片后,将图片分成了三块,明、暗、阴影区域

2.明区域使用空白进行填充

3.阴影区域使用横线进行填充

4.暗区域使用数字进行填充,通过对暗区域的像素进行分类,不同像素使用不同数字进行填充即可

三、程序源码

#!/usr/bin/env python
# encoding: utf-8
import cv2
import random
import numpy as np

class digitalPicture:
   '''
    This is a main Class, the file contains all documents.
    One document contains paragraphs that have several sentences
    It loads the original file and converts the original file to new content
    Then the new content will be saved by this class
   '''
   def __init__(self):
       self.picture = 'assets/aaa.jpeg'

def hello(self):
       '''
       This is a welcome speech
       :return: self
       '''
       print('*' * 50)
       print(' ' * 20 + '数字成像')
       print(' ' * 5 + 'Author: autofelix  Date: 2022-01-06 13:14')
       print('*' * 50)
       return self

def run(self):
       '''
       The program entry
       '''
       img = cv2.imread(self.picture)
       str_img = self.img_to_string(img)
       cv2.imwrite('result.jpg', str_img)
       print('处理完成!!!!')

def img_to_string(self, frame, K=6):
       """
       利用 聚类 将像素信息聚为3或5类,颜色最深的一类用数字密集地表示,阴影的一类用“-”横杠表示,明亮部分空白表示。
       ---------------------------------
       frame:需要传入的图片信息。可以是opencv的cv2.imread()得到的数组,也可以是Pillow的Image.read()。
       K:聚类数量,推荐的K为3或5。根据经验,3或5时可以较为优秀地处理很多图像了。若默认的K=5无法很好地表现原图,请修改为3进行尝试。若依然无法很好地表现原图,请换图尝试。 ( -_-|| )
       ---------------------------------
       聚类数目理论可以取大于等于3的任意整数。但水平有限,无法自动判断当生成的字符画可以更好地表现原图细节时,“黑暗”、“阴影”、”明亮“之间边界在哪。所以说由于无法有效利用更大的聚类数量,那么便先简单地限制聚类数目为3和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
       # 得到labels(类别)、centroids(矩心)。
       # 如第一行6个像素labels=[0,2,2,1,2,0],则意味着6个像素分别对应着 第1个矩心、第3个矩心、第3、2、3、1个矩心。
       compactness, labels, centroids = cv2.kmeans(frame_array, K, None, criteria, 10, flags)
       centroids = np.uint8(centroids)

# labels的数个矩心以随机顺序排列,所以需要简单处理矩心.
       centroids = centroids.flatten()
       centroids_sorted = sorted(centroids)
       # 获得不同centroids的明暗程度,0最暗
       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转变为实际的明暗程度列表,0最暗。
       labels = centroids_index[labels]
       # 列表解析,每2*2个像素挑选出一个,组成(height*width*灰)数组。
       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)  # 创建长宽为原图三倍的白色画布。

# 因为 字体大小为0.45时,每个数字占6*6个像素,而白底画布为原图三倍
       # 所以 需要原图中每2*2个像素中挑取一个,在白底画布中由6*6像素大小的数字表示这个像素信息。
       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__':
   digitalPicture().hello().run()

来源:https://blog.csdn.net/weixin_41635750/article/details/122416171

标签:Python,数字成像
0
投稿

猜你喜欢

  • ASP中CACHE缓存技术

    2010-05-03 10:58:00
  • 如何利用python实现列表嵌套字典取值

    2023-07-08 02:16:50
  • PHP实现web socket长链接流程详解

    2023-05-27 23:44:39
  • 关于python pygame游戏进行声音添加的技巧

    2023-06-25 16:12:30
  • python机器学习实战之K均值聚类

    2022-12-22 21:50:31
  • JS数组方法concat()用法实例分析

    2024-04-29 14:08:20
  • 用SQL语句生成带有小计合计的数据集脚本

    2009-01-06 11:33:00
  • Python中的 if 语句及使用方法

    2022-12-19 16:35:10
  • MySql使用mysqldump 导入与导出方法总结

    2024-01-23 13:26:32
  • Python详解argparse参数模块之命令行参数

    2021-06-20 21:16:18
  • flask项目集成swagger的方法

    2022-08-04 09:00:48
  • Python 解决相对路径问题:"No such file or directory"

    2022-03-16 21:45:05
  • 怎样修改 MySQL数据库中的密码

    2008-11-27 15:35:00
  • 如何修改vue-treeSelect的高度

    2024-05-08 09:33:55
  • Python探索之ModelForm代码详解

    2022-05-16 06:14:33
  • Windows10安装Oracle19c数据库详细记录(图文详解)

    2024-01-23 20:13:15
  • python基于Node2Vec实现节点分类及其可视化示例详解

    2022-04-16 06:49:33
  • 基于Python实现微博抓取GUI程序

    2021-06-01 10:59:29
  • python模块之re正则表达式详解

    2021-08-15 03:34:52
  • Mysql事务特性和级别原理解析

    2024-01-25 11:00:04
  • asp之家 网络编程 m.aspxhome.com