Image format conversion in Python image processing library pil

Source: Internet
Author: User
Tags image processing library

O

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:

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.

The picture we sampled in this article is a photograph of Lena:

Mode "1":

  

>>> fromPILImportImage>>> Lena = Image.open ("lena.bmp")>>>Lena.mode'RGB'>>>Lena.getpixel ((0,0)) (226, 137, 125)>>> 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>>> Lena_1.show ()

Results:

  

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.

  

 >>> lena_l = Lena.convert ( l   )  >>> Lena_l.mode   " l   '  >>>

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:

  

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

>>> lena_p = Lena.convert ("P")>>> lena_p.mode'  P'>>> lena_p.getpixel ((0,0))105

Results:

  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.

>>> Lena_rgba = Lena.convert ("rgba")>>> >>> >> > >>> lena_rgba.mode'rgba'>>> lena_rgba.getpixel (( 0,0) (226, 137, 255)>>> lena_rgba.getpixel (0,1) (226,137, 255) >>> lena_rgba.show ()

  

  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.

  

>>> Lena_cmyk = Lena.convert ("CMYK")>>> Lena_cmyk.mode  'CMYK'>>> lena_cmyk.getpixel ((0,0)) (118,0) >>> Lena_cmyk.getpixel ((0,1)) (118,0)>>> lena_cmyk.show ()

 

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:

 

  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.

>>> lena_ycbcr = Lena.convert ("ycbcr")>>> Lena_ Ycbcr.mode'ycbcr'>>> lena_ycbcr.getpixel ((0,0)) (162, 107, 173 )>>> lena.getpixel ((0,0)) (226, 137,)

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:

  

  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.

  

>>> lena_i = Lena.convert ("I")>>> lena_i.mode'  I'>>> lena_i.getpixel ((0,0))162>>> Lena_i.getpixel ((0,1)) 162>>> lena_l = Lena.convert ("L")>>> Lena_ L.getpixel ((0,0))162>>> Lena_l.getpixel ((0,1)) 162

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.

  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.

  

>>> Lena_f = Lena.convert ("F")>>> lena_f.mode'  F'>>> lena_f.getpixel ((0,0))162.2429962158203>>> Lena_ F.getpixel ((0,1))

  

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.

Summary: The mode of conversion in CSDN on the article written in a very detailed, here I based on the above carding concept, please note, please refer to:

50843172

Image format conversion in Python image processing library pil

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.