python basis: implementation of the Python image processing library PIL image format conversion

This article describes the implementation of the Python image processing library PIL image format conversion, the paper sample code described in great detail, has a certain reference value of learning for all of us to learn or work, we need friends to below as the small series learn it together
in digital image processing, it has its specific processing algorithms for different image formats. So, before doing image processing, we need to think clearly realize what image format they have to be based algorithm design and. Based on this demand, using python PIL library of image processing to achieve the conversion of different image formats.

For color images, regardless of image format is PNG, or BMP, or JPG, in the PIL, a module open Image () function to open mode image of an object is returned "RGB". For a grayscale image, regardless of its image format is PNG, or BMP, or JPG, after opening, the pattern is "L".

Before introduction through blog Image modules for mutual conversion between PNG, BMP, JPG format color image can be accomplished by the module Open Image (), and save () function. Specifically that, when you open these images, PIL will decode them as "RGB" image of the three-channel. Users can be based on the "RGB" image, process it. Processed using the function Save (), the processing result may be saved as PNG, BMP, JPG in any format. This also completes the conversion between several formats. Similarly, other color image format conversion can be done in this way. Of course, for different grayscale image format can also be completed by a similar route, but is decoded after PIL pattern is "L" image.

Here, I would like to explain in detail convert Image Module () function for converting between different image modes.

The Convert () function is defined in three forms, which form is defined as follows:

im.convert(mode)⇒image
im.convert(“P”, **options)⇒image
im.convert(mode, matrix)⇒image

Using different parameters, the current image into a new pattern, and generates a new image as a return value.

In this paper we sampled picture is a photo of lena:

Mode "1":

>>> from PIL import Image
>>> 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))
>>> lena_1.getpixel((10,10))
>>> lena_1.getpixel((10,120))
>>> lena_1.getpixel((130,120))
>>> lena_1.show()

Result: The Here Insert Picture Description
mode "L":

Mode "L" is a gray image, for each pixel with its eight bit, where 0 represents black and 255 represents white, other numbers represent different shades of gray. In the PIL, the pattern "RGB" is converted to "L" mode is converted according to the following formula:

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

Below we will lena image is converted to "L" image.

>>> lena_L = lena.convert("L")
>>> lena_L.mode
'L'
>>> lena_L.size
(512, 512)
>>> lena_L.getpixel((0,0))
>>> lena.getpixel((0,0))
(226, 137, 125)
>>> lena_L.show()
>>> lena_L.save("lena_l.bmp")
>>>

For the first pixel in the original image is lena (197, 111, 78), which is converted to gray values:

197 * 299/1000 + 111 * 587/1000 + 78 * 114/1000 = 132.952, PIL only take the integer part, i.e. 132.

Lena_L image converted as follows: Here Insert Picture Description
Model P:

Pattern "P" 8-bit color images, each pixel is represented by its 8 bit, with a value corresponding to the color palette according to the check out.

Here we use the default color palette will lena image into a "P" picture.

example:

>>> lena_P = lena.convert("P")
>>> lena_P.mode
'P'
>>> lena_P.getpixel((0,0))

Result: The Here Insert Picture Description
Mode "RGBA":

Mode "RGBA" is a 32-bit color images, each pixel is represented by its 32 'bit, 24bit which represent red, green and blue three channels, additional 8bit represents the alpha channel, i.e., clear channel.

We'll mode to "RGB" image of lena converted to "RGBA" image.

>>> lena_rgba = lena.convert("RGBA")
>>> 
>>> 
>>> 
>>> lena_rgba.mode
'RGBA'
>>> lena_rgba.getpixel((0,0))
(226, 137, 125, 255)
>>> lena_rgba.getpixel((0,1))
(226, 137, 125, 255)
>>> lena_rgba.show()

Here Insert Picture Description
Mode "CMYK":

Mode "CMYK" is a 32-bit color images, each pixel is represented by its 32 bit. Mode "CMYK" is printed CMYK mode, it is a mode used when the chromatic color printing, RGB color mixing using the principle of pigment, with black ink, for a total of four colors mixed superimposed to form a so-called "full-color printing." .

Four standard colors is: C: Cyan = cyan, also known as 'blue' or 'blue' M: Magenta = Magenta, also known as 'Magenta'; Y: Yellow = yellow; K: Key Plate ( blacK) = positioned registering color (black).

Below we will convert lena picture mode to "RGB" for "CMYK" image.


>>> lena_cmyk = lena.convert("CMYK")
>>> lena_cmyk.mode
'CMYK'
>>> lena_cmyk.getpixel((0,0))
(29, 118, 130, 0)
>>> lena_cmyk.getpixel((0,1))
(29, 118, 130, 0)
>>> lena_cmyk.show()

PIL can be learned from the example in "RGB" is converted to "CMYK" formula is as follows:

= 255 C - R & lt
M = 255 - G
the Y = 255 - B
K = 0
due to the conversion formula is relatively simple, the converted color image some distortion.

Lena_cmyk image converted as follows: Here Insert Picture Description
Mode "YCbCr":

Mode "YCbCr" 24-bit color images, each pixel is represented by its 24 bit. Wherein Y refers to YCbCr luminance component, Cb refers to the blue chrominance components, and red chrominance component Cr means. The human eye is more sensitive to the Y component video, thus reducing the chrominance component by sub-sampling the chrominance component, the naked eye can not detect changes in image quality.

Mode "RGB" is converted to "YCbCr" formula is as follows:
the Y = 0.257 R & lt + 0.504 G + 0.098 B + 16
Cb = -0.148
R & lt 0.291- G + 0.439 B + 128
of Cr = 0.439 R & lt 0.368- G + B-0.071 * 128

below we will mode to "RGB" image of lena converted to "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, 125)
>>>

According to the equation
the 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
of Cr = 0.439 197-0.368 111-0.071 * 78 + 128 = 168.097

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

Lena_ycbcr image converted as follows: Here Insert Picture Description
Mode "I"

Pattern "I" is a 32-bit integer gray image, with each of its 32 bit represents a pixel, 0 represents black and 255 represents white, a number between (0,255) represented by different shades of gray. In the PIL, the pattern "RGB" is converted to "I" mode is converted according to the following formula:

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

We'll mode to "RGB" image of lena converted to "I" picture.

>>> lena_I = lena.convert("I")
>>> lena_I.mode
'I'
>>> lena_I.getpixel((0,0))
>>> lena_I.getpixel((0,1))
>>> lena_L = lena.convert("L")
>>> lena_L.getpixel((0,0))
>>> lena_L.getpixel((0,1))

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

Mode "F"

Mode "F" 32-bit floating gray image, with each of its 32 bit pixels, where 0 represents black and 255 represents white, a number between (0,255) represented by different shades of gray. In PIL, the transition from mode "RGB" to "F" is a conversion mode in accordance with the following formula: F. = R & lt * 299/1000 + G * + B * 114/1000 587/1000
Let the mode is "RGB" lena image is converted to the "F" image.

>>> lena_F = lena.convert("F")
>>> lena_F.mode
'F'
>>> lena_F.getpixel((0,0))
162.2429962158203
>>> lena_F.getpixel((0,1))
162.2429962158203
>>>

Here Insert Picture Description
Mode "F" mode and "L" is the same as the conversion formula, the formula is converted to RGB gray values, but the pattern "F" retained fractional part, such as the experiment data.

These are the details of image processing library implemented in Python PIL image format conversion of
content on more than how many, and finally to recommend a good reputation in the number of public institutions [programmers], there are a lot of old-timers learning skills, learning experience, interview skills, workplace experience and other share, the more we carefully prepared the zero-based introductory information, information on actual projects, the timing has to explain the Python programmer technology every day, share some learning methods and the need to pay attention to small detailsHere Insert Picture Description

Published 35 original articles · won praise 1 · views 10000 +

Guess you like

Origin blog.csdn.net/chengxun02/article/details/105029806