Python image grayscale conversion and Image array operations, python Image Grayscale Array

Source: Internet
Author: User

Python image grayscale conversion and Image array operations, python Image Grayscale Array

Using python and numpy to complete a series of basic image processing by directly operating the Image array

Numpy introduction:

NumPy is a well-known Python scientific computing toolkit, which contains a large number of useful tools, such as array objects (used to represent vectors, matrices, images, etc.) and linear algebra functions.

An array object can be used to perform important operations in an array, such as matrix product, transpose, equation solving system, vector product, and normalization. This provides a foundation for image deformation, modeling changes, image classification, and image clustering.

In the previous python basic image operation, when loading an image, the image is converted to a NumPy array object by calling the array () method. The array objects in NumPy are multidimensional and can be used to represent vectors, matrices, and images. Through direct operations on the array of images, you can complete a lot of image processing.

Numpy-related knowledge has a lot of information on the Internet. As the foundation of python scientific computing, it is worth studying carefully.

Basic image operations using image Arrays:

Recognition Image array:

Through the following programs, we can see an array of images with grayscale images, as well as a segment of the numpy array.

#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # Read the Image and convert it to an array im = array (Image. open (". /source/test.jpg ") # print im. shape, im. dtype # print im [100,100, 0] # print im [100,100] of the rgb value of output coordinate 100,100 and print im of the type output at coordinate 100,100 and color channel r. shape, im. dtype

Running result:

(600,500, 3) uint8
64
[64 117 195]

We see a three-dimensional array, which represents the abscissa, ordinate, and color channels.

We can exchange red and blue channels through arrays.

#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # Read the Image and convert it to an array im = array (Image. open (". /source/test.jpg ") # Red Channel r = im [:,:, 0] # swap the red and blue channels and display im [:, 0] = im [:, :, 2] im [:,:, 2] = rimshow (im) show ()

The numpy array slicing method is used here. There are a lot of information about numpy on the Internet.

Running result:

 

In the process of converting to an array, we can set the data type, and the Image array of the grayscale image is also meaningful:

#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # Read the Image, grayscale, and convert it to array im = array (Image. open (". /source/test.jpg "). convert ('l'), 'F') # print im of the dimension length and type of the output array. shape, im. dtype # print im [100,100]

Running result:

(600,500) float32
110.0

The extra parameter 'F' converts the Data Type of the array to a floating point number.

The grayscale image has no color information, so the shape tuples have only two values.

* The opposite operation of array () transformation can be completed using fromarray () of PIL, for example, im = Image. fromarray (im)

Simple Application of Image Arrays-grayscale Transformation:

Grayscale image:

A grayscale digital image is an image with only one sampling color per pixel. This type of image is usually displayed as a gray scale from the shortest black to the brightest white.

You can use the following methods to convert an image to a gray scale:

1. Floating Point Algorithm: Gray = R * 0.3 + G * 0.59 + B * 0.11

2. Integer method: Gray = (R * 30 + G * 59 + B * 11)/100

3. Shift Method: Gray = (R * 76 + G * 151 + B * 28)> 8;

4. Average Method: Gray = (R + G + B)/3;

5. Green only: Gray = G;

After obtaining Gray through any of the above methods, replace R, G, and B in the original RGB (R, G, B) with Gray to form a new color RGB (Gray, gray, Gray), use it to replace the original RGB (R, G, B) is a grayscale image.

It has been used many times before. You can use convert ('l') to obtain Grayscale Images Using python.

Grayscale conversion:

After reading images into NumPy array objects, we can perform any mathematical operations on them. A simple example is the grayscale transformation of the image. That is, the function f converts 0... 255 intervals (or 0... 1) ing to itself.

The following program has some simple grayscale transformations:

#-*-Coding: UTF-8-*-from PIL import Imagefrom pylab import * # Read the Image, grayscale, and convert it to array im = array (Image. open (". /source/test.jpg "). convert ('l') im2 = 255-im # reverse image processing im3 = (100.0/255) * im + 100 # transform the image pixel value to 100... 200 interval im4 = 255.0 * (im/255.0) ** 2 # The image obtained after the pixel value is calculated by Square (quadratic function transformation to make the darker pixel value smaller) #2x2 display result use the first display original grayscale image subplot (221) title ('f (x) = x') gray () imshow (im) #2x2 display result: Use the second display inverse diagram subplot (222) title ('f (x) = 255-x') gray () imshow (im2) #2x2 display result use third display 100-200 figure subplot (223) title ('f (x) = (100/255) * x + 100') gray () imshow (im3) #2x2 display result use the fourth display quadratic function transform graph subplot (224) title ('f (x) = 255 * (x/255) ^ 2') gray () imshow (im4) # print int (im. min (), int (im. max () print int (im2.min (), int (im2.max () print int (im3.min (), int (im3.max () print int (im4.min ()), int (im4.max () show ()

Running result:

 

0 255
0 255
100 200
0 255

You can see the result of the gray scale transformation. The second image is displayed in reverse phase. The dark part of the third image is highlighted and the bright part is dimmed. The value is limited to 100 to 200, the last image uses quadratic function transformation to make the darker pixel values darker.

Conclusion:

This blog introduces how python uses image arrays to perform image operations, including several simple examples. Through arrays, we can perform any mathematical operations on images, it is the foundation of image deformation, image classification, and image clustering. I hope my blog will help you ~

Articles you may be interested in:
  • Tutorial on using the PIL module to process images in Python
  • Implementation of Reversed colors for python Image Processing
  • Image implementation in python Image Processing
  • Basic tutorial on using Pillow to process images in Python Programming

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.