Home >Backend Development >Python Tutorial >What is the code for converting python images to character drawings?

What is the code for converting python images to character drawings?

coldplay.xixi
coldplay.xixiOriginal
2021-01-05 10:36:355179browse

Python picture to character drawing code: first calculate the grayscale value corresponding to the color of the picture; then according to the grayscale value, obtain the character corresponding to each pixel in the picture from the character set, the code is [args = parser .parse_args()].

What is the code for converting python images to character drawings?

The operating environment of this tutorial: Windows 7 system, python version 3.9, DELL G3 computer. This method is suitable for all brands of computers.

Related free learning recommendations: python video tutorial

Principle

1. Calculate the grayscale value corresponding to the color of the picture. The calculation formula is as follows

gray = 0.2126 * r + 0.7152 * g + 0.0722 * b

2. According to the grayscale value, obtain the characters corresponding to each pixel in the image from the character set

Code

# !/usr/bin/env python
# -*- coding:utf-8 -*-
from PIL import Image
import argparse
#命令行输入参数处理
parser = argparse.ArgumentParser()
parser.add_argument('file')   #输入文件
parser.add_argument('-o', '--output')  #输出文件
parser.add_argument('--width', type = int, default = 50) #输出字符画宽
parser.add_argument('--height', type = int, default = 50) #输出字符画高
#获取参数
args = parser.parse_args()
IMG = args.file
WIDTH = args.width
HEIGHT = args.height
OUTPUT = args.output
# 字符画使用的字符集
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`&#39;. ")
def get_char(r,g,b,alpha = 256):
  """将256灰度映射到70个字符上"""
  if alpha == 0:
    return &#39; &#39;
  length = len(ascii_char)
  # 计算灰度的公式
  gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
  unit = (256.0 + 1)/length
  index=int(gray/unit)
  return ascii_char[index]
if __name__ == &#39;__main__&#39;:
  im = Image.open(IMG)
  im = im.resize((WIDTH,HEIGHT), Image.NEAREST)
  txt = ""
  # 获取每个像素点对应的字符
  for i in range(HEIGHT):
    for j in range(WIDTH):
      txt += get_char(*im.getpixel((j,i)))
    txt += &#39;\n&#39;
  print(txt)
  #字符画输出到文件
  if OUTPUT:
    with open(OUTPUT,&#39;w&#39;) as f:
      f.write(txt)
  else:
    with open("output.txt",&#39;w&#39;) as f:
      f.write(txt)z

Running results

What is the code for converting python images to character drawings?

What is the code for converting python images to character drawings?

The above is the detailed content of What is the code for converting python images to character drawings?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn