Python image processing is a simple, easy-to-learn and powerful interpreted programming language. It has a concise and clear syntax and an efficient high-level data structure, allowing you to easily and effectively implement object-oriented programming, the following describes how to process Python images.
Of course, first of all, I would like to thank "Love Butterfly" for his article "Using Python image processing", which helped me firmly solve the problem with Python and PIL, for some introduction and basic operations on PIL, please refer to this article. Here I will mainly introduce my experience in the use process.
PIL can be used to convert the color of an Image, and supports 24-bit color, 8-bit grayscale, and Binary Map modes. after the convert (mode) function is complete, mode indicates the color mode of the output. For example, ''l'' indicates grayscale, and ''1'' indicates the Binary Graph mode.
However, when the convert function is used to convert a grayscale image to a binary image, a fixed threshold value of 127 is used, that is, the pixel value above 127 is 1, the pixel value of gray scale less than 127 is 0. The Image. point function is used to convert gray-scale images to two-value images through custom thresholds.
- In-depth analysis of Python Syntax Functions
- In-depth description of Python application features
- Study on Python Database
- Python developers
- Analysis of Python Dynamic Language
The Image. point function can be used in multiple forms. Only Image. point (table, mode) is discussed here. This function can be used to convert pixel colors by using a look-up table. The table is the ing table in the color conversion process. Each color channel should have 256 elements, and the mode indicates the output color mode. Similarly, ''l'' indicates the gray level, ''1'' indicates the two-value graph mode.
It can be seen that the key to the conversion process is to design the ing table. If you only need a Simple clamping value, you can set the elements in the table above or below the clamping value to 1 and 0 respectively. Of course, because the table here does not have any special requirements, you can implement a one-to-one ing relationship in the range of 0,255 by setting special elements.
The sample code is as follows:
- import Image
-
- # load a color image
- im = Image.open(''fun.jpg'')
-
- # convert to grey level image
- Lim = im.convert(''L'')
- Lim.save(''fun_Level.jpg'')
-
- # setup a converting table with constant threshold
- threshold = 80
- table = []
- for i in range(256):
- if i < threshold:
- table.append(0)
- else:
- table.append(1)
-
- # convert to binary image by the table
- bim = Lim.point(table, ''1'')
-
- bim.save(''fun_binary.jpg'')
IT usually involves a lot of tasks, but few resources are needed to support these tasks. This has become a public secret. Any IT solution that promises to improve coding efficiency and reduce software costs should be carefully considered. A significant advantage of Python image processing is that it can save a lot of money in the Software creation and maintenance phase of enterprises, the software costs in these two phases account for 50% to 95% of the total cost of the entire software lifecycle.
The clear and readable Syntax of Python makes the software code exceptionally easy to understand, and even the strong feeling for programmers who are not initially engaged in and developing the original project. Although some programmers oppose the extensive use of spaces in Python code.
However, almost everyone acknowledges that Python image processing is far more readable than C or Java, the latter two adopt special character mark code block structures, loops, functions, and other programming structures to start and end. Those who advocate Python also claim that using these characters may produce significant differences in programming style, making it difficult for those who are responsible for maintaining the code to read the code.