How to Use Python image processing?

Source: Internet
Author: User

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:

 
 
  1. import Image  
  2.  
  3. # load a color image  
  4. im = Image.open(''fun.jpg'')  
  5.  
  6. # convert to grey level image  
  7. Lim = im.convert(''L'')  
  8. Lim.save(''fun_Level.jpg'')  
  9.  
  10. # setup a converting table with constant threshold  
  11. threshold = 80 
  12. table = []  
  13. for i in range(256):  
  14.     if i < threshold: 
  15.         table.append(0)  
  16.     else:  
  17.         table.append(1)  
  18.  
  19. # convert to binary image by the table  
  20. bim = Lim.point(table, ''1'')  
  21.  
  22. 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.

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.