Image Format Conversion in Python image processing library PIL (2)
2. convert other modes to "RGB"
The "RGB" mode is a 24-bit color image. Each pixel is represented by 24 bits, indicating the red, green, and blue channels respectively.
In PIL, after a color image is opened, it is converted to the "RGB" mode. Then, this mode can be converted to another mode, for example, the "1", "L", "P", and "RGBA" modes can also be converted to the "RGB" mode.
1. Switch Mode "1" to mode "RGB"
After the "RGB" mode is converted to the "1" mode, the pixels are either 0 or 255 points in black and white. When "1" is switched to "RGB", the three channels of "RGB" are copies of the pixel values of "1.
Example:
>>> from PILimport Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_1 =lena.convert("1")>>> lena_1_rgb =lena_1.convert("RGB")>>> lena.getpixel((0,0))(197, 111, 78)>>>lena_1.getpixel((0,0))255>>>lena_1_rgb.getpixel((0,0))(255, 255, 255)
2. Convert the mode "L" to the mode "RGB"
After the mode "RGB" is converted to the mode "L", the pixel value is a value between [0,255. When the mode "L" is converted to "RGB", the three channels of "RGB" are copies of the pixel values of the mode "L.
Example:
>>> from PIL import Image>>> lena = Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_L =lena.convert("L")>>> lena_L_rgb =lena_L.convert("RGB")>>>lena.getpixel((0,0))(197, 111, 78)>>>lena_L.getpixel((0,0))132>>>lena_rgb.getpixel((0,0))(132, 132, 132)
3. Convert the mode "P" to the mode "RGB"
After the mode "RGB" is converted to the mode "P", the pixel value is a value between [0,255], but it is the index value of the palette, And it is eventually a color image. When converting the mode "P" to "RGB", the three channels of "RGB" will change to the color value of the pixel value index of the mode "P.
Example:
>>> from PIL import Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_P =lena.convert("P")>>> lena_P_rgb =lena_P.convert("RGB")>>>lena.getpixel((0,0))(197, 111, 78)>>>lena_P.getpixel((0,0))62>>>lena_P_rgb.getpixel((0,0))(204, 102, 51)
4. Convert the mode "RGBA" to the mode "RGB"
After the "RGB" mode is converted to the "RGBA" mode, the image is changed from three channels to four channels. The values of the R, G, and B channels remain unchanged, and the new alpha channels are all 255, indicates opacity. When the mode "RGBA" is converted to "RGB", the three channels of "RGB" are converted back to the original values.
Example:
>>> from PILimport Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_RGBA =lena.convert("RGBA")>>> lena_RGBA_rgb= lena_RGBA.convert("RGB")>>>lena.getpixel((0,0))(197, 111, 78)>>>lena_RGBA.getpixel((0,0))(197, 111, 78, 255)>>>lena_RGBA_rgb.getpixel((0,0))(197, 111, 78)
5. Convert the mode "CMYK" to the mode "RGB"
After the "RGB" mode is converted to the "CMYK" mode, the image is changed from three channels to four channels. The values of the three channels C, M, and Y are calculated using the previous formula, the value of channel K is 0.
C = 255-R
M = 255-G
Y= 255-B
K = 0
When converting from the "CMYK" mode to "RGB", the three channels of "RGB" are converted back to the original values, which is lossless conversion.
R= 255-C
G = 255-M
B = 255-Y
Example:
>>> from PIL import Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_CMYK =lena.convert("CMYK")>>> lena_CMYK_rgb= lena_CMYK.convert("RGB")>>>lena.getpixel((0,0))(197, 111, 78)>>>lena_CMYK.getpixel((0,0))(58, 144, 177, 0)>>>lena_CMYK_rgb.getpixel((0,0))(197, 111, 78)
6. Convert the mode "YCbCr" to the mode "RGB"
The mode "RGB" is converted to the mode "YCbCr", which is usually calculated using the following formula, but PIL does not strictly follow this formula.
Y = 0.257 * R + 0.564 * G + 0.098 * B + 16
Cb =-0.148 * R-0.291 * G + 0.439 * B + 128
Cr = 0.439 * R-0.368 * G-0.071 * B + 128
When converting from the "YCbCr" mode to "RGB" mode, it is usually calculated according to the following formula, but PIL does not strictly follow this formula.
R = 1.164 * (Y-16) + 1.596 * (Cr-128)
G = 1.164*(Y-16)-0.392*(Cb-128)-0.813 * (Cr-128)
B = 1.164 * (Y-16) + 2.017 * (Cb-128)
Example:
>>> from PILimport Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_YCbCr =lena.convert("YCbCr")>>> lena_YCbCr_rgb= lena_YCbCr.convert("RGB")>>>lena.getpixel((0,0))(197, 111, 78) >>>lena_YCbCr.getpixel((0,0))(132, 97, 173)>>>lena_YCbCr_rgb.getpixel((0,0))(195, 110, 77)
7. Convert the mode "I" to the mode "RGB"
The mode "RGB" is converted to the mode "I", and the three channels are converted to a single channel. The following formula is used to calculate the pixel value:
I = R * 299/1000 + G * 587/1000 + B * 114/1000
When switching from the "I" mode to the "RGB" mode, the three channels of the "RGB" mode are copies of the pixel values of the "I" mode.
Example:
>>> from PILimport Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_I =lena.convert("I")>>> lena_I_rgb =lena_I.convert("RGB")>>>lena.getpixel((0,0))(197, 111, 78)>>>lena_I.getpixel((0,0))132>>>lena_I_rgb.getpixel((0,0))(132, 132, 132)
8. Convert the mode "F" to the mode "RGB"
The "RGB" mode is converted to the "F" mode, and the color image is converted into a 32-bit floating point gray image. In PIL, the conversion from the "RGB" mode to the "F" mode is based on the following formula:
F = R * 299/1000 + G * 587/1000 + B * 114/1000
When converting from the "F" mode to the "RGB" mode, the three channels of the "RGB" mode are copies of the pixel value integer part of the "F" mode.
Example:
>>> from PILimport Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_F =lena.convert("F")>>> lena_F_rgb =lena_F.convert("RGB")>>>lena.getpixel((0,0))(197, 111, 78)>>> lena_F.getpixel((0,0))132.95199584960938>>>lena_F_rgb.getpixel((0,0))(132, 132, 132)
Iii. Conversion of color palette Images
When PIL converts an "RGB" image to a "P" image, the convert () function is defined as follows:
Im. convert ("P", ** options )? Image
This definition sets the pattern to "P", followed by several optional parameters. They are dither, palette, and colors.
The dither parameter is used to control color jitter. The default value is floydsteberger. If this function is not enabled, the value is set to NONE.
The palette parameter is used to control the generation of the palette. The default value is WEB, which is the standard "web palette" in 216 colors ". To use an optimized color palette, the value is ADAPTIVE.
The colors parameter controls the number of colors in the palette. When the palette parameter is ADAPTIVE, the colors value indicates the number of colors in the palette. The default value is the maximum value, that is, 256 colors.
Use the default value to convert "RGB" to "P" mode image as follows:
The default value of dither is floydsteberger. If the color jitter function is not enabled, the value is set to NONE. The conversion result is as follows:
The palette parameter is WEB by default. To use an optimized color palette, the value is ADAPTIVE. The conversion result is:
When the palette parameter is ADAPTIVE, the colors value indicates the number of colors in the palette. The default value is 256. When colors is set to 10, the conversion result is:
The preceding conversion code is as follows:
>>> from PILimport Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_P_dither= lena.convert("P", dither = Image.NONE)>>> lena_P_palette= lena.convert("P", palette = Image.ADAPTIVE)>>>lena_P_palette_colors = lena.convert("P", palette = Image.ADAPTIVE,colors = 10)
Iv. matrix-based mode conversion
The third definition of the Mode Conversion Function convert () is as follows:
Im. convert (mode, matrix )? Image
This definition is only suitable for converting an "RGB" image to an "L" or "RGB" image, and cannot be converted to an image in other modes. The variable matrix is 4 or 16 tuples.
Example: The following example converts an RGB image (linear calibration based on ITU-R709, using D65 brightness) to the cie xyz color space:
>>>from PIL import Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena.mode'RGB'>>> rgb2xyz= ( 0.412453,0.357580, 0.180423, 0, 0.212671,0.715160, 0.072169, 0, 0.019334,0.119193, 0.950227, 0 ) >>>lena_L = lena.convert("L", rgb2xyz)>>>lena_rgb = lena.convert("RGB", rgb2xyz)
The image lena_L is as follows:
The image lena_rgb is as follows:
V. Summary
PIL converts the image format and pattern, which are relatively simple. Users can convert the image to the target mode based on their own needs for various processing. For different image processing purposes, it is extremely critical to choose which mode to design the image algorithm and what algorithm to design. I hope that I will have a deep understanding in my later studies.
(End)