Image Format Conversion in Python image processing library PIL (1)

Source: Internet
Author: User
Tags image processing library

Image Format Conversion in Python image processing library PIL (1)

In digital image processing, there are specific processing algorithms for different image formats. Therefore, before performing image processing, we need to consider the format of the image we want to design and implement the algorithm. Based on this requirement, this article uses the image processing library PIL in python to convert different image formats.

For color images, whether the Image format is PNG, BMP, or JPG, after the open () function of the Image module is enabled in PIL, the format of the returned image object is "RGB ". For Grayscale Images, whether the image format is PNG, BMP, or JPG, the mode is "L ".

Through the introduction of the Image module in the previous blog, mutual conversion between PNG, BMP, and JPG color Image formats can be completed through the open () and save () Functions of the Image module. Specifically, when these images are opened, PIL decodes them into three-channel "RGB" images. Users can process the "RGB" image. After processing, save () can save the processing result to any format in PNG, BMP, and JPG. This completes the conversion between several formats. Similarly, color images in other formats can be converted in this way. Of course, for Grayscale Images of different formats, it can also be done in a similar way, but after PIL decoding, It is an image in the "L" mode.

Here, I want to introduce in detail the convert () function of the Image module for conversion between images of different modes.

Convert () functions are defined in three forms:

Im. convert (mode )? Image

Im. convert ("P", ** options )? Image

Im. convert (mode, matrix )? Image

Use different parameters to convert the current image to a new mode and generate a new image as the return value.

Through the blog "Introduction to the basic concepts of Python image processing library PIL", we know that there are nine different PIL models. 1, L, P, RGB, RGBA, CMYK, YCbCr, I, F, respectively.

The sample image I used in this article is a classic lena image in image processing. The lena image with the resolution of 512x512 is as follows:

1. Convert the mode "RGB" to another mode

1. mode "1"

The mode "1" is a binary image, which is not black or white. However, each pixel is represented by 8 bits. 0 indicates black, and 255 indicates white. Next we will convert the lena image to the "1" image.

Example:

>>>from PIL import Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena.mode'RGB'>>> lena.getpixel((0,0))(197, 111, 78)>>> lena_1 = lena.convert("1")>>> lena_1.mode'1'>>> lena_1.size(512, 512)>>>lena_1.getpixel((0,0))255>>> lena_1.getpixel((10,10))255>>>lena_1.getpixel((10,120))0>>>lena_1.getpixel((130,120))255

The image lena_1 adopts the "1" mode and the resolution is 512x512, as shown below:

2. mode "L"

Mode L is a gray image. Each pixel is represented by 8 bits. 0 indicates black, 255 indicates white, and other numbers indicate different gray levels. In PIL, the conversion from the "RGB" mode to the "L" mode is based on the following formula:

L = R * 299/1000 + G * 587/1000 + B * 114/1000

Next we will convert the lena image to an "L" image.

Example:

>>> from PIL importImage>>> lena = Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena.mode'RGB'>>> lena.getpixel((0,0))(197, 111, 78)>>> lena_L =lena.convert("L")>>> lena_L.mode'L'>>> lena_L.size(512, 512)>>>lena.getpixel((0,0))(197, 111, 78)>>>lena_L.getpixel((0,0))132

For the first pixel, the original image lena is (197,111, 78), and its conversion to the gray value is:

197*299/1000 + 111*587/1000 + 78*114/1000 = 132.952, PIL only takes the integer part, that is, 132.

The converted image lena_L is as follows:

3. mode "P"

The "P" mode is an 8-bit color image. Each pixel is represented by 8 bits, and the corresponding color value is queried by the color palette.

Next we use the default palette to convert the lena image to a "P" image.

Example:

>>> from PIL importImage>>> lena = Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena.mode'RGB'>>> lena.getpixel((0,0))(197, 111, 78)>>> lena_P =lena.convert("P")>>> lena_P.mode'P'>>>lena_P.getpixel((0,0))62

 

The converted image lena_P is as follows:

4. Pattern "RGBA"

The pattern "RGBA" is a 32-bit color image. Each pixel is represented by 32 bits, where 24 bits represent the red, green, and blue channels, and 8 bits represent the alpha channels, that is, transparent channels.

Next we will convert the lena image in the "RGB" mode to the "RGBA" image.

Example:

                                                                                                                              >>> from PIL import Image>>>lena = Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>>lena.mode'RGB'>>>lena.getpixel((0,0))(197,111, 78)>>>lena_rgba = lena.convert("RGBA")>>>lena_rgba.mode'RGBA'>>>lena_rgba.getpixel((0,0))(197,111, 78, 255)>>>lena_rgba.getpixel((0,1))(196,110, 77, 255)>>>lena.getpixel((0,0))(197,111, 78)>>>lena.getpixel((0,1))(196,110, 77)

 

As you can see from the instance, when the current method is used to convert the "RGB" image to the "RGBA" image, all alpha channels are set to 255, that is, completely opaque.

The converted image lena_rgba is as follows:

5. mode "CMYK"

The "CMYK" mode is a 32-bit color image. Each pixel is represented by 32 bits. The "CMYK" mode is the four-color printing mode. It is a set color mode used for color printing. It uses the three-color Color Mixing Principle of the color material and the black ink to combine the four colors, the so-called "Full Color Printing" is formed ".

The four standard colors are: C: Cyan = Cyan, also known as 'day Blue' or 'Blue m': Magenta = product red, also known as 'magenta '; Y: Yellow = Yellow; k: Key Plate (blacK) = positioning color (blacK ).

Next we will convert the lena image in the "RGB" mode to the "CMYK" image.

Example:

>>>from PIL import Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_cmyk =lena.convert("CMYK")>>> lena_cmyk.mode'CMYK'>>>lena_cmyk.getpixel((0,0))(58, 144, 177, 0)>>> lena_cmyk.getpixel((0,1))(59, 145, 178, 0)>>>lena.getpixel((0,0))(197, 111, 78)>>>lena.getpixel((0,1))(196, 110, 77)

 

You can see from the instance that the formula for converting "RGB" to "CMYK" in PIL is as follows:

C = 255-R
M = 255-G
Y= 255-B
K = 0

Because the conversion formula is relatively simple, the color of the converted image is somewhat distorted.

The converted image lena_cmyk is as follows:

6. mode "YCbCr"

The pattern "YCbCr" is a 24-bit color image, each of which is represented in 24 bits. In YCbCr, Y indicates the brightness component, Cb indicates the blue color component, and Cr indicates the red color component. The human eye is more sensitive to the Y component of the video. Therefore, after subsampling the color component to reduce the color component, the human eye will detect invisible changes in the image quality.

The formula for converting the mode "RGB" to "YCbCr" is as follows:

Y = 0.257 * R + 0.504 * 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

Next we will convert the lena image in the "RGB" mode to the "YCbCr" image.

Example:

>>>from PIL import Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>> lena_ycbcr =lena.convert("YCbCr")>>>lena_ycbcr.mode'YCbCr'>>>lena_ycbcr.getpixel((0,0))(132, 97, 173)>>>lena.getpixel((0,0))(197, 111, 78)

 

According to the formula, Y = 0.257*197 + 0.564*111 + 0.098*78 + 16 = 136.877

Cb =-0.148*197-0.291*111 + 0.439*78 + 128 = 100.785
Cr = 0.439*197-0.368*111-0.071*78 + 128 = 168.097

Therefore, PIL does not convert "RGB" to "YCbCr" according to this formula.

The converted image lena_ycbcr is as follows:

7. mode "I"

The mode "I" is a 32-bit gray image. Each pixel is represented by 32 bits, 0 indicates black, 255 indicates white, and (0,255) indicates different gray scales. In PIL, the conversion from the "RGB" mode to the "I" mode is based on the following formula:

I = R * 299/1000 + G * 587/1000 + B * 114/1000

Next we will convert the lena image in the "RGB" mode to the "I" image.

Example:

>>> from PIL import Image>>>lena = Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>>lena.getpixel((0,0))(197,111, 78)>>>lena.getpixel((0,1))(196,110, 77)>>> lena_I =lena.convert("I")>>> lena_I.mode'I'>>>lena_I.getpixel((0,0))132>>>lena_I.getpixel((0,1))131>>> lena_L =lena.convert("L")>>>lena_L.getpixel((0,0))132>>>lena_L.getpixel((0,1))131

From the experiment results, the results of the "I" and "L" modes are exactly the same, but the pixels of the "L" mode are 8 bits, the pixel of the "I" mode is 32 bits.

8. Mode "F"

The pattern "F" is a 32-bit floating-point gray image. Each pixel is represented by 32 bits, 0 indicates black, 255 indicates white, and (0,255) indicates different gray scales. 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

Next we will convert the lena image in the "RGB" mode to the "F" image.

Example:

>>>from PIL import Image>>> lena =Image.open("D:\\Code\\Python\\test\\img\\lena.jpg")>>>lena.getpixel((0,0))(197, 111, 78)>>>lena.getpixel((0,1))(196, 110, 77)>>> lena_F =lena.convert("F")>>> lena_F.mode'F'>>>lena_F.getpixel((0,0))132.95199584960938>>>lena_F.getpixel((0,1))131.95199584960938

The conversion formulas for the "F" mode and the "L" mode are the same. They are all the formulas for converting RGB to the gray value. However, the "F" Mode retains the fractional part, such as the data in the experiment.

(To be continued)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.