Conversion of RGB values to images (python PIL ),
Today, I saw a misc question. To obtain a txt file with an RGB value, I need to convert the RGB value to an image. Specific question: here
For the first time I encountered this type of question, I made a record. Here I use the python PIL Image Library.
Specific ideas:
1. First, determine the size of the image, that is, the width and height.
The number of rows in the txt file (61366 = 2*61*503, because the last row is empty, so it is not within the calculation range)
We can obtain the following sizes: 503*2 (the x and y values do not change much, but the difference between the horizontal and vertical values)
So we will try the conversion of these three different sizes. It is of practical significance to view the converted image (Ah, hello, the last one is so exaggerated. It's not good to think about it :)
2. The next step is to use the python image library for conversion. The code of the primary node is attached.
#-*-Coding: UTF-8-*-from PIL import Imageimport rex = 503 # x coordinate the number of lines in the txt by integer decomposition y = 122 # y coordinate x * y = number of lines im = Image. new ("RGB", (x, y) # create an image file = open('misc100.txt ') # open an rbg value file # generate an image for I in range (0, x): for j in range (0, y): line = file. readline () # obtain a row of rgb = line. split (",") # Separate rgb im. putpixel (I, j), (int (rgb [0]), int (rgb [1]), int (rgb [2]) # converting rgb to pixel im. show ()
X and y must be replaced by the preceding size. The result is as follows:
A copy of the python PIL learning manual is attached.