Home  >  Article  >  Backend Development  >  Detailed explanation of how to use Python to convert images into character paintings

Detailed explanation of how to use Python to convert images into character paintings

黄舟
黄舟Original
2017-08-22 13:24:342691browse

This article mainly introduces the example of converting pictures into character paintings in Python. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.

Character painting is really interesting. Character painting is generated by replacing the pixels in the picture with characters.

But pixels have different colors. How do we encode pixels with different colors into corresponding characters?

Conversion method:

  • Convert color images into grayscale images

  • According to the color depth RGB value (the value range is from 0 to 255, where 0 is black and 255 is white)

  • Involves your favorite character set

  • According to the character set sequence and character set length, the RGB value is encoded into the corresponding character.

RGB

RGB color mode is based on three color channels: red (R), green (G), and blue (B). RGB represents the colors of the three channels of red, green, and blue. This standard includes almost all colors that can be perceived by human vision.

Normally, RGB each has 256 levels of brightness, expressed numerically from 0, 1, 2... until 255. Note that although the highest number is 255, 0 is also one of the values, so there are 256 levels in total.

Grayscale image

Grayscale image refers to an image that only contains brightness information and does not contain color information, just like the black and white photos we usually see: brightness From dark to light, the change is continuous.

Therefore, to represent a grayscale image, the brightness value needs to be quantized. It is usually divided into 256 levels from 0 to 255, of which 0 is the darkest (completely black) and 255 is the brightest (completely white). In the method of expressing color, in addition to RGB, the conversion formula from RGB in color pictures to gray value Gray is:


#在PIL中,从模式“RGB”转换为“L”模式(灰度模式)
Gray = 0.299R+0.587G+0.114B

For example, we use 26 lowercase English letters as Our character set. The character set capacity is 26 (the value interval width corresponding to one character = 256/character set length)

The interval width here is 256/26=9.8),

The corresponding relationship between gray and the character set :

Gray interval corresponding characters


[0.0, 9.8)这|a
[9.8, 19.6)|b
[19.6, 29.4)|c
...|...
[225.6, 235.4]|x
[235.4, 245.2]|y
[245.2, 255.0]|z

RGB conversion function


char_string = 'abcdefghijklmnopqrstuvwxyz'

def rgb2char(r, g, b):
  length = len(char_string)
  gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)

  # 每个字符对应的gray值区间宽度
  unit = (256.0 + 1) / length

  # gray值对应到char_string中的位置(索引值)
  idx = int(gray / unit)
  return char_string[idx]

Preprocessing

If the size is too large or too small, we will not be able to recognize the character paintings when we open the txt file. So you need to adjust the image size appropriately first. Note here that you can change the scaling coefficient delta coefficient as needed


from PIL import Image

#预处理(将图片尺寸压缩,并转为灰度图) 
def preprocess(img_path,delta=100):
  img = Image.open(img_path) 
  # 获取图片尺寸
  width, height = img.size
  # 获取图片最大边的长度 if width > height:
    max = width
  else:
    max = height

  # 伸缩倍数scale
  scale = max / delta
  width, height = int(width / scale), int(height / scale)
  img = img.resize((width, height)) 
  return img

Picture to character

Read the picture and obtain it according to the coordinates An rgb tuple of the pixel, encoded as the characters


def img2char(img_obj, savepath):
  txt = ''
  width, height = img_obj.size
  # 获取像素点的rgb元组值,如(254, 0, 0),并将其转化为字符
  for i in range(height):
    line = ''
    for j in range(width):
      line += rgb2char(*img_obj.getpixel((j, i)))
    txt = txt + line + '\n'

  # 保存字符画
  with open(savepath, 'w+', encoding='utf-8') as f:
    f.write(txt)




img_obj = preprocess(img_path)
img2char(img_obj, savepath)

Insert image

##Change char_string and transform the effect you want

The above is the detailed content of Detailed explanation of how to use Python to convert images into character paintings. 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