Image format conversion in Python image processing library pil (i)

Source: Internet
Author: User
Tags image processing library

In Digital image processing, there are specific processing algorithms for different image formats. So, before we do the image processing, we need to think about the format of the image we want to design and implementation of the algorithm. Based on this requirement, this paper uses the image processing library pil in Python to realize the conversion of different image formats.

For color images, regardless of whether the image format is PNG, or BMP, or JPG, in PiL, the mode of the returned Image object is "RGB" when opened using the Image module's open () function. And for grayscale images, regardless of whether the image format is PNG, or BMP, or JPG, when opened, its mode is "L".

Through the previous blog's introduction to the image module, the conversion between PNG, BMP, and JPG color image formats can be done through the open () and save () functions of the image module. Specifically, when these images are opened, PiL decodes them into three-channel "RGB" images. The user can process this "RGB" image based on it. After processing, use the function Save () to save the processing results in any format in PNG, BMP, and JPG. This also completes the conversion between several formats. Similarly, color images in other formats can be converted in this way. Of course, for different formats of grayscale images can also be done in a similar way, just PIL decoding is the pattern of "L" image.

Here, I would like to describe in detail the convert () function of the image module for conversion between different pattern images.

The Convert () function has three types of definitions, which are defined as follows:

The Convert () function has three types of definitions, which are defined as follows:

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 produce a new image as the return value.

We know that there are nine different modes in PiL, through the blog "Introduction to the basic concepts of Python image processing library pil." The 1,l,p,rgb,rgba,cmyk,ycbcr,i,f are respectively.

The example image I used in this article is the classic Lena photo in image processing. The Lena image with resolution 512X512 is as follows:

One, the mode "RGB" conversion to other different modes

1, Mode "1"

The pattern "1" is a two-value image, not black or white. But it is represented by 8 bits per pixel, 0 for black, and 255 for white. Below we convert the Lena image to a "1" image.

Example:

>>> fromPILImportImage>>> 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 mode is "1" and the resolution is 512x512, as follows:

2, Mode "L"

The pattern "L" is a gray image with 8 bits per pixel, 0 for black, 255 for white, and other numbers for different shades of gray. In PiL, the conversion from mode "RGB" to "L" mode is based on the following formula:

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

Below we convert the Lena image to an "L" image.

Example:

>>> fromPIL 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 point, the original image is Lena (197, 111, 78), which is converted to a gray value:

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

The converted image is lena_l as follows:

3, Mode "P"

The mode "P" is a 8-bit color image with 8 bits per pixel, and its corresponding color values are queried by color palette.

Below we use the default palette to convert the Lena image to a "P" image.

Example:

>>> fromPIL 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 is lena_p as follows:

4. Mode "RGBA"

The mode "RGBA" is a 32-bit color image with 32 bits per pixel, with 24bit representing Red, green, and blue three channels, and 8bit representing the alpha channel, the Transparent channel.

Below we convert the Lena image of "RGB" mode to "RGBA" image.

Example:

>>> fromPILImportImage>>>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 example, when you convert an "RGB" image to an "RGBA" image using the current method, the alpha channel is all set to 255, which is completely opaque.

The converted image is Lena_rgba as follows:

5, Mode "CMYK"

The pattern "CMYK" is a 32-bit color image, which is represented by 32 bits per pixel. Mode "CMYK" is printing four color separations mode, it is a color printing used in a pattern, the use of color material of the three-color mixing principle, plus black ink, a total of four colors mixed overlay, forming the so-called "full-color printing."

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) = position registration color (dark).

Below we convert the Lena image of the mode "RGB" to "CMYK" image.

Example:

>>> fromPILImportImage>>> 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)

From the example, you can learn that the formula "RGB" in PiL is converted to "CMYK" as follows:

C = 255-r
M = 255-g
Y = 255-b
K = 0

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

The converted image is Lena_cmyk as follows:

6, Mode "YCbCr"

The mode "YCbCr" is a 24-bit color image, which is represented by 24 bits per pixel. YCbCr where y refers to the luminance component, CB refers to the Blue Chroma component, and CR refers to the Red chroma component. The human eye is more sensitive to the Y component of the video, so the visual quality of the image will not be perceptible to the naked eye after sub-sampling the chroma component to reduce the chroma component.

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

Below we convert the Lena image of the pattern "RGB" 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)) (, 173 >>>lena.getpixel ((0,0)) ( 197, 111, min) 

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

Thus, the PIL is not in accordance with this formula for "RGB" to "YCbCr" conversion.

The converted image is lena_ycbcr as follows:

7. Mode "I"

The pattern "I" is a 32-bit integer gray image with 32 bits per pixel, 0 for black, 255 for White, and (0,255) for different shades of gray. In PiL, the conversion from the mode "RGB" to the "I" mode is based on the following formula:

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

Below we convert the Lena image of the pattern "RGB" to the "I" image.

Example:

>>> fromPILImportImage>>>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))>>>lena_i.getpixel (0,1))    131 >>> lena_l =lena.convert ("L")    >>>Lena_l.getpixel ((0,0))>>>lena_l.getpixel (0,1))    131

From the experimental results, the pattern "I" is exactly the same as the result of the pattern "L", except that the pixel of the pattern "L" is 8bit, and the pixel of mode "I" is 32bit.

8, Mode "F"

The pattern "F" is a 32-bit floating-point gray image with 32 bits per pixel, 0 for black, 255 for White, and (0,255) for different shades of gray. In PiL, the conversion from mode "RGB" to "F" mode is based on the following formula:

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

Below we convert the Lena image of the pattern "RGB" to an "F" image.

Example:

>>> fromPILImportImage>>> 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 pattern "F" is the same as the conversion formula for the mode "L", which is the formula that RGB converts to a gray value, but the pattern "F" retains the fractional part, such as the data in the experiment.

(This article is reproduced in CSDN) For more information please visit https://blog.csdn.net/column/details/pythonpil.html

Image format conversion in Python image processing library pil (i)

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.