From ffd021f12e08b4a263c7efcbb4b2494b6f6ac805 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=81=E5=B0=98?= Date: Wed, 27 May 2020 15:08:16 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=9B=BE=E7=89=87=E8=BD=AC=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E7=94=BB.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...54\345\255\227\347\254\246\347\224\273.md" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 "\345\233\276\347\211\207\350\275\254\345\255\227\347\254\246\347\224\273.md" 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 0000000..e7047e9 --- /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 -- Gitee