码迷,mamicode.com
首页 > 编程语言 > 详细

利用python把图片转换成代码

时间:2018-11-10 12:33:44      阅读:455      评论:0      收藏:0      [点我收藏+]

标签:row   ati   数组   bic   bsp   port   修改   break   size   

详细步骤如下:

#导入cv模块
import cv2 as cv
import random
import numpy as np
# # =================================存取图像=====================================
# 60号宋体字母占用字符比较
xiangsi={
    "I":198,"V":229,"L":230,"U":235,"C":236,"Y":236,"T":245,"J":248,"A":262,"X":265,"Z":270,"N":276,"G":281,"F":284,"P":303,"O":306,"E":322,"K":339,"D":357,"W":373,"R":380,"H":384,"Q":391,"B":391,"S":292,"M":432
}

I_value=180   # I映射为不同的值表示亮度,
M_value=50   # M映射为50
x=(M_value-I_value)/(xiangsi[M]-xiangsi[I])
b=I_value-xiangsi[I]*x
#归一化,后数组递减排列
for zimu in xiangsi:
    xiangsi[zimu]=int(x*xiangsi[zimu]+b)   # 将字母表示为对应的黑度值。

print(xiangsi)

# # 直接读取单通道灰度图
gray_img = cv.imread(4.jpg, cv.IMREAD_GRAYSCALE)
gray_img = cv.resize(gray_img,(int(gray_img.shape[1]),int(gray_img.shape[0]/1.7)),interpolation=cv.INTER_CUBIC)
# cv.imshow(‘iker‘, gray_img)
# cv.waitKey(0)
print(gray_img.shape)   # 先是高度, 再是宽度
file = open(1.txt,mode=w)



for row in range(gray_img.shape[0]):
    for col in range(gray_img.shape[1]):
        pzimu=" "
        cansee=False
        for zimu in xiangsi:
            if (gray_img[row][col] > xiangsi[zimu]):
                file.write(pzimu)
                cansee=True
                break
            else:
                pzimu=zimu
        if(not cansee):file.write(pzimu)
    file.write(\n)
file.close()

//////*****如果包导不进去,就导opencv-python*******///////

原图:技术分享图片

 

 

效果图:

技术分享图片

 

注意两点:

1图片像素太大,可以用画图,修改像素大小

2.通过调整文本格式里的格式(小六,西欧文)

 

利用python把图片转换成代码

标签:row   ati   数组   bic   bsp   port   修改   break   size   

原文地址:https://www.cnblogs.com/542684416-qq/p/9938515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!