Python to complete simple image processing tasks

Source: Internet
Author: User
Tags bmp image

Abstract:This article briefly introduces how to use python to complete simple image processing tasks. Although the best tool for image processing is the MATLAB Image Processing Toolbox, python is more advantageous when performing some "simple" image processing tasks or a large number of simple image processing tasks.

Keywords:Image processing image enhancement Python

Abstract:This context introduces some methods used to fullfill simply image processing task in Python programming language. although the best image processing toolkit is the Image Processing Toolbox of Matlab, python is superior to this Toolkit when your staff is somewhat "simple" or simple but boring and exhausting image processing commitment.

Keywords:Image processing image enhancement Python

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 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:

Import image

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:

Import image

IMG = image.open(“dip.jpg ")

This will return an image instance object, and all subsequent operations are completed on the IMG. Here, the image we read is:


(2) Adjust the image size:

Import image

IMG = image. Open ("img.jpg ")

New_img = IMG. Resize (128,128), image. bilinear)

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:

Import image

IMG = image. Open ("img.jpg ")

New_img = IMG. Resize (128,128), image. bilinear)

Rot_img = new_img.rotate (45)

Rot_img.save ("rot_img.jpg ")

Therefore, the image we save to rot_img.jpg looks like the following:


(4) format conversion:

Assume that we want to convert the generated rot_img.jpg image to a BMP image. To do this, we just need to add the following line after the above Code:

Rot_img.save ("con_img.bmp ")

If the Save format is not specified, Pil will automatically convert the formats based on the filename suffix, isn't it easy?

(5) histogram statistics:

The histogram () method of the image class instance can calculate the histogram data and return the result as a list. For example, we perform histogram statistics on the image generated after rotation:

Import image

IMG = image. Open ("img.jpg ")

New_img = IMG. Resize (128,128), image. bilinear)

Rot_img = new_img.rotate (45)

Print rot_img.histogram ()

After running, the statistical value of the number of all 256 gray-level pixels is printed:

[2819, 22, 82,119,186,204,212,218,223,200,151,103,129, 74, 80, 83,110, 70, 59, 64, 59, 58, 35, 45, 42, 38, 32, 39, 33, 19, 24, 26, 32, 17, 33, 24, 34, 19, 18, 15, 11, 23, 16, 15, 21, 13, 20, 22, 27, 10, 29, 26, 18, 16, 28, 18, 26, 37, 36, 25, 28, 36, 28, 31, 22, 20, 15, 13, 15, 18, 12, 15, 21, 21, 12, 18, 17, 12, 11, 18, 16, 14, 21, 20, 18, 19, 15, 20, 22, 16, 22, 15, 23, 26, 16, 8, 13, 19, 30, 16, 15, 11, 22, 12, 14, 8, 10, 14, 13, 8, 12, 22, 11, 13, 18, 16, 21, 21, 14, 14, 11, 14, 15, 9, 23, 19, 15, 17, 9, 10, 11, 12, 14, 16, 9, 17, 15, 20, 14, 18, 18, 32, 34, 55, 54, 51, 72, 78, 83, 99,118,171,138,177,191,158,159,123,106,136,121,121,148,137,118,145,150,150,133, 98,111,118,111,104,129,124,104,144,126,118,133,124,108, 87, 87, 83, 85, 75, 76, 75, 62, 84, 46, 61, 54, 54, 63, 45, 54, 66, 46, 52, 51, 49, 51, 52, 62, 50, 67, 72, 53, 53, 83, 54, 39, 57, 31, 53, 31, 38, 38, 42, 31, 29, 38, 39, 39, 26, 43, 36, 45, 68, 57, 60, 42, 39, 41, 38, 46, 44, 40, 47, 57, 45, 59, 53, 59, 81, 78, 75, 95, 46, 62, 1, 0, 0]

Summary: complete.

4. imagedraw module:

The imagedraw module provides basic graphics capabilities. The graphic capabilities here refer to the drawing capabilities. The Pil Library provides a wealth of graphic rendering functions that can draw straight lines, arcs, rectangles, polygon, ovans, slices, and so on. Imagedraw implements a draw class, and all the drawing functions are implemented in the method of the draw class instance. It is easy to instantiate a draw class instance:

Import image, imagedraw

IMG = image. Open ("img.jpg ")

Draw = imagedraw. Draw (IMG)

First, import the imagedraw module. Then, because the Drawing operation is performed on the image, the image object IMG must be passed to the draw class constructor through parameters when instantiating the draw class. Now, you can call various draw methods to draw images on IMG.

(1) draw a straight line:

Import image, imagedraw

IMG = image. Open ("img.jpg ")

Draw = imagedraw. Draw (IMG)

Width, height = IMG. Size

Draw. Line (255), (width-1, height-1), fill =)

Draw. Line (0, height-1), (width-1, 0), fill = 255)

IMG. Save ("cross_line.jpg ")

Explain the above Code:

The first three lines will not be explained here. Width, height = IMG. size is the size of the IMG. The main purpose of getting these two attributes is to use them in the following two lines of code:

Draw. Line (255), (width-1, height-1), fill =)

Draw. Line (0, height-1), (width-1, 0), fill = 255)

The two lines of code draw two straight lines in the two diagonal lines of the IMG image. At the very end, we saved the animated line image as cross_line.jpg. The final effect is shown below:


(2) Draw circles:

Import image, imagedraw

IMG = image. Open ("img.jpg ")

Width, height = IMG. Size

Draw = imagedraw. Draw (IMG)


IMG. Save ("circle.jpg ")

Compared with the code that draws the diagonal line above, only one line is changed, namely:


Note:

(0, 0, width-1, height-1) specifies the line of the drawn arc;


Fill = 255 specifies the color of the drawn line. Note: Outline = 255 should be used in the Pil document, but I found that the arc color can only be specified with fill = 255.

The image after the circle is drawn:


Summary: operations on drawing are similar, so here is a brief introduction. For details, see pil-handbook.pdf.

5. imageenhance module:

This module provides a commonly used image enhancement toolbox. It can be used for color enhancement, brightness enhancement, contrast enhancement, image regionalization, and other enhancement operations. All operations have the same form of interface-implemented through the enhance method of the corresponding class: color enhancement is implemented through the enhance method of the color class; brightness enhancement is implemented through the enhance method of the brightness class; contrast enhancement is implemented through the enhance method of the contrast class, and regionalization is implemented through the enhance method of the sharpness class. All operations must pass an image object as a parameter to the class constructor. This parameter defines the enhancement object. At the same time, all operations return a new image object. If the parameter passed to the enhance method is 1.0, no changes are made to the original image and a copy of the original image is returned. Below are some simple examples:

(1) brightness enhancement:

Import image, imageenhance

IMG = image. Open ("img.jpg ")

Brightness = imageenhance. brightness (IMG)

Bright_img = brightness. Enhance (2.0)

Bright_img.save ("bright.jpg ")

Note:

Brightness = imageenhance. brightness (IMG)

This line transmits IMG to the brightness class to obtain a brightness class instance;

Bright_img = brightness. Enhance (2.0)

This line calls the enhance method of the brightness instance. The input parameter is used to increase the brightness by two times;

We finally get to bright.jpg and the image looks like this:

 

The image on the right is the image before enhancement (original image). Note that the brightness difference between the two is very large.

(2) image regionalization:

Import image, imageenhance

IMG = image. Open ("img.jpg ")

Sharpness = imageenhance. Sharpness (IMG)

Sharp_img = sharpness. Enhance (7.0)

Sharp_img.save ("bright.jpg ")

This code is similar to the above Code, so we will not explain it too much here. Let's take a look at the effect comparison before and after the enhancement:

 

The image on the right is the original image before enhancement. Note that the degree of regionalization between the two is very different.

(3) contrast enhancement:

Import image, imageenhance

IMG = image. Open ("img.jpg ")

Contrast = imageenhance. Contrast (IMG)

Contrast_img = contrast. Enhance (2.0)

Contrast_img.save ("contrast.jpg ")

Same as above, we only look at the effect comparison before and after the enhancement:

 

Obviously, the contrast of the enhanced image (left) is higher than that of the original image (right.

Conclusion: Using imageenhance for image enhancement is effective and simple. Of course, for fine and complex image enhancement operations, the functions provided here are not powerful enough, but when performing simple image enhancement operations, a simple and easy solution is undoubtedly very attractive.

6. Conclusion:

In batch processing or simple image processing tasks, using a combination of Python and Pil (Python Image Library) 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.

References:

1. Digital Image Processing (second edition); gangsps; e-Industry Press; 2003;

2. Python Image Library handbook; Fredrik lundh, Matthew Ellis; pythonware. Inc; 2002;

3. Python documentation (release 2.3.3); Guido van rosum; 2003;

4.www.python.org

5.www.pythonware.org

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.