Summary of common Python Image Processing Techniques

Source: Internet
Author: User

The Python programming language brings us great benefits in practical application. Its ease-of-use benefits greatly attract the attention of most developers. Here we will experience the benefits of this language through the related methods of Python image processing.

  • How to Implement the Socket service using Python
  • Learn about the ease-of-use features of sending emails in Python
  • Python dictionary addition and deletion operations
  • A variety of common Python dictionary Application Methods
  • Python PDB simple debugging method

Although the best tool for image processing is the MATLAB Image Processing Toolbox, when performing some "simple" image processing tasks or a large number of simple image processing tasks, the Python image processing method is more advantageous.

1. Introduction:

When it comes to image processing, people usually think of MATLAB as a tool. Indeed, MATLAB provides a powerful toolbox for image processing. However, for simple image processing tasks, using an advanced language will get twice the result with half the effort. Python is undoubtedly the ideal choice for implementing this function. Python's object-oriented and weak data types make it simple and convenient to use for simple image processing.

2. Introduction:

PythonWare provides the free Python Image processing toolkit PIL (Python Image Library), which provides basic Image processing functions, such as changing the Image size, rotating the Image, and converting the Image format, color field space conversion, image enhancement, histogram processing, interpolation and filtering, etc. Although this software package is not suitable for implementing complex image processing algorithms similar to MATLAB, Python's rapid development capabilities and object-oriented features make it very suitable for prototype development.

In PIL, any Image is represented by an Image object, and this class is exported by a module with the same name as it. Therefore, you need to load an Image, the simplest form is as follows:

 
 
  1. import Image   
  2. img = Image.open(“dip.jpg”)  

Note: The Image in the first line is the module name, the img in the second line is an Image object, and the Image class is defined in the Image module. Do not confuse the Image module with the Image class. Now we can perform various operations on the img, and all operations on the img will eventually be reflected on the dip. img image.

PIL provides a wide range of functional modules: Image, ImageDraw, ImageEnhance, and ImageFile. The most common modules are Image, ImageDraw, and ImageEnhance. Next I will introduce this separately. For more information about the use of other modules, see instructions. For the PIL software package and related instructions, see the PythonWare site www.Pythonware.com.

3. Image module:

The Image module is the most basic module of PIL. The Image class is exported, and an Image class instance object corresponds to an Image. The Image module also provides many useful functions.

1) open an image file:

 
 
  1. import Image   
  2. img = Image.open(“dip.jpg”)  

This will return an Image instance object, and all subsequent operations are completed on the img.

2) Adjust the image size:

 
 
  1. import Image   
  2. img = Image.open("img.jpg")   
  3. new_img = img.resize((128,128),Image.BILINEAR)   
  4. new_img.save("new_img.jpg")  

The original image size is 128x128.

This is so simple. It should be noted that Image. BILINEAR specifies to use the BILINEAR method for pixel interpolation.

3) Rotating images:

Now we rotate the adjusted image 45 degrees:

 
 
  1. import Image   
  2. img = Image.open("img.jpg")   
  3. new_img = img.resize((128,128),Image.BILINEAR)   
  4. rot_img = new_img.rotate(45)   
  5. rot_img.save("rot_img.jpg")  

Summary:

Using the combination of Python and PILPython Image Library in batch processing or simple Python Image processing tasks is a good choice. Imagine there is a task to increase the contrast by 2 times for all images in a folder. It is very simple to use Python. Of course, I have to admit that Python is still relatively weak in image processing, and it is obviously not suitable for more complex applications such as filtering and feature extraction. My personal opinion is that when you want to implement these "advanced" algorithms, let's hand it over to MATLAB. However, if you are only facing an image processing task that generally does not require complex algorithms, Python should be your best partner.

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.