首页 > 其他分享 >MathModelling

MathModelling

时间:2022-10-04 16:55:24浏览次数:48  
标签:tmp gray MathModelling height width im txt

记录一下数学建模的学习过程~

-10.4
python可以识别图片的灰度并转化为字符画

from PIL import Image
im = Image.open("Cal2.bmp")
width = im.size[0]
height = im.size[1]
# width = 200
# height = 200
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")

def getc(r, g, b, alpha = 256):
    if(alpha == 0):
        return ' '
    length = len(ascii_char)
    gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
    unit = (256.0 + 1) / length
    return ascii_char[int(gray / unit)]

fh = open("Cal.txt", "w")
# im = im.resize((width, height), Image.NEAREST)
txt = ""
x1=""
y1=""
for i in range(height):
    for j in range(width):
        tmp = im.getpixel((j,i))
        gray = int(0.2126 * tmp[0] + 0.7152 * tmp[1] + 0.0722 * tmp[2])
        # print(gray)
        if(gray == 0):
        # if(tmp[0] > 0 or tmp[1] > 0 or tmp[2] > 0):
            x1 += str(j) + ','
            y1 += str(i) + ','
        txt += getc(*im.getpixel((j,i)))
    txt += '\n'
fh.write(txt)
fh.close()

标签:tmp,gray,MathModelling,height,width,im,txt
From: https://www.cnblogs.com/SkyRainWind/p/16754033.html

相关文章