diff --git "a/\345\233\276\347\211\207\350\275\254\345\255\227\347\254\246\347\224\273.md" "b/\345\233\276\347\211\207\350\275\254\345\255\227\347\254\246\347\224\273.md" new file mode 100644 index 0000000000000000000000000000000000000000..e7047e9a5ac93e19f9f3b5fdb3a1912c3f749082 --- /dev/null +++ "b/\345\233\276\347\211\207\350\275\254\345\255\227\347\254\246\347\224\273.md" @@ -0,0 +1,38 @@ +``` +""" +图片转字符画 +版本: python version >= 3 +扩展: +pip install --upgrade pip +pip install --user matplotlib +pip install pillow +""" + +import matplotlib.pyplot as plt +#显示区域的长宽 +show_width = 150 +show_height = 50 + +ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ") +char_len = len(ascii_char) + +# 读取图片,这里要指定图片路径 +pic = plt.imread("E:/1.jpg") +pic_height ,pic_width,_ = pic.shape +#获取图像的高、宽 + +gray = 0.2126 * pic[:,:,0] + 0.7152 * pic[:,:,1] + 0.0722 * pic[:,:,2] +#RGB转灰度图的公式 gray = 0.2126 * r + 0.7152 * g + 0.0722 * b + +#思路就是根据灰度值,映射到相应的ascii_char +for i in range(show_height): + #根据比例映射到对应的像素 + y = int(i * pic_height / show_height ) + text = "" + for j in range(show_width): + x = int(j * pic_width / show_width) + text += ascii_char[int(gray[y][x] / 256 * char_len)] + print(text) +``` + +![截图](https://gitee.com/lovefc/images/raw/master/20200527.png "屏幕截图.png") \ No newline at end of file