Use Python's PIL to output an array of images

I recently got a city map in txt format, the format is a 2400 × 2400 two-dimensional array, the value of each array is an integer, from 1 to 800, the city is divided into 800 different districts, an integer of districts With the same value, it is not easy to view the txt file directly, so consider converting the txt to an image using python.

The normal color image can be regarded as a three-dimensional array, length × width × channel, where the channel is the data used to represent the color on each pixel, for example, RGB three channels use three 0 ~ 255 values ​​to describe red and green , Blue three colors. PIL.Image opens PNG, BMP and JPG color image formats and saves them as a three-dimensional array in RGB mode.

But the current txt file is a two-dimensional array, only the length × width, need to fill the RGB color channel values. Since there are 800 different numbers, we take the area value modulo 255 as the B channel value and round it as the G channel value. This is enough to distinguish the values ​​of 800 areas, but the colors of adjacent areas are too close Therefore, the modulus value is first multiplied by 50 and then the modulus is 255, so that the color value of the adjacent area can be pulled apart on the R channel for easy observation.

import numpy as np
from PIL import Image

map_data = np.loadtxt("./city_map.txt")
map_data = map_data.tolist()

# 填充地图数组的颜色通道
for i in range(2400):
    for j in range(2400):
        value = int(map_data[i][j])
        if value == 0:  # 如果地图值为0,显示黑色
            map_data[i][j] = [0, 0, 0]
        else:           # 将流量低的区域根据区域号码来填充RGB三通道
            r = (value % 255) * 50 % 255        # 取模乘以50再取模作为R通道
            g = (value // 255) * 50 + 100       # 取整加100作为G通道的值
            b = value % 255                     # 取模作为B通道的值
            map_data[i][j] = [r, g, b]

# 输出png图像
map_data = np.array(map_data)
map_data = np.asarray(map_data, np.uint8)
pic = Image.fromarray(map_data)
pic.save('city.png')

The generated image is as follows:

If you need to generate a light to dark image according to the different index values ​​of each area, first read the index flow_value of the area from the index array flow_data [] according to the index of the area, and then divide the flow_value and the index maximum value max_flow and then multiply Take 255, so that the indicator value is mapped to the value of 0 to 255 in the R channel, and then subtract the value from 255, dye the G and B channels to 255, so that the place where the indicator value is small is displayed in white .

    map_data = np.loadtxt("./city.txt")
    map_data = map_data.tolist()
    max_flow = flow_data.max()
    for i in range(2400):
        for j in range(2400):
            area = int(map_data[i][j])
            flow_value = flow_data[area - 1]   # 根据地图区域索引读取值
            if area == 0:  # 如果地图值为0,显示黑色
                map_data[i][j] = [0, 0, 0]
            else:  # 根据区域流量值来填充RGB三通道
                r = 255 - flow_value / max_flow * 255
                g = 255
                b = 255
                map_data[i][j] = [r, g, b]
    # 输出png图像
    map_data = np.array(map_data)
    map_data = np.asarray(map_data, np.uint8)
    pic = Image.fromarray(map_data)
    pic.save('./output/' + flow_type + '_flow_map.png')

There are nine modes for generating images using PIL's Image object, in addition to RGB, there are the following:

  1. Mode "1" is a binary image, either black or white. But each pixel is represented by 8 bits, 0 means black, and 255 means white.
  2. Mode "L" is a gray image, each pixel of which is represented by 8 bits, 0 means black, 255 means white, and other numbers indicate different gray levels
  3. Mode "I" is a 32-bit integer gray image, each pixel of which is represented by 32 bits, 0 represents black, 255 represents white, and the number between (0,255) represents different gray levels.
  4. Mode "F" is a 32-bit floating-point gray image, each pixel of which is represented by 32 bits, 0 for black, 255 for white, and numbers between (0,255) indicate different gray levels.
  5. Mode "P" is an 8-bit color image, each pixel of which is represented by 8 bits, and the corresponding color value is queried according to the color palette.

  6. The mode "RGBA" is a 32-bit color image, each pixel of which is represented by 32 bits, of which 24 bits represent three channels of red, green, and blue, and 8 bits represent an alpha channel, which is a transparent channel.

  7. The mode "CMYK" is a 32-bit color image, and each pixel is represented by 32 bits. The mode "CMYK" is a four-color printing mode. It is a chromatic mode used in color printing. It uses the principle of three primary colors mixing of colorants and black ink to mix and superimpose a total of four colors to form a so-called "full color printing" . The four standard colors are: C: Cyan = cyan, also known as 'sky blue' or 'blue' M: Magenta = magenta, also known as 'magenta'; Y: Yellow = yellow; K: Key Plate ( blacK) = positioning register (black).

  8. The mode "YCbCr" is a 24-bit color image, and each pixel is represented by 24 bits. YCbCr where Y refers to the luminance component, Cb refers to the blue chrominance component, and Cr refers to the red chrominance component. The human eye is more sensitive to the Y component of the video, so after sub-sampling the chroma component to reduce the chroma component, the naked eye will not notice the change in image quality.

For example, image.convert ("F") can convert the picture to F mode

 

 

Published 124 original articles · Like 65 · Visit 130,000+

Guess you like

Origin blog.csdn.net/theVicTory/article/details/104514921