


Python image grayscale transformation and image array operation methods
This article mainly introduces the related information of Pythonimage grayscale transformation and imagearrayoperation. Friends in need can refer to the following
Using python and numpy Complete a series of basic image processing by directly operating image arrays
Introduction to numpy:
NumPy is a very famous Python scientific computing toolkit , which contains a large number of useful tools, such as arrays objects (used to represent vectors, matrices, images, etc.) and linear algebra functions .
Array objects can implement important operations in arrays, such as matrix products, transposes, solving equation systems, vector products, and normalization. This provides the basis for image deformation, modeling changes, image classification, image clustering, etc.
In the previous article on basic python image operations, when loading an image, the image is converted into a NumPy array object by calling the array() method. Array objects in NumPy are multidimensional and can be used to represent vectors, matrices, and images. A lot of image processing can be done by directly operating on arrays of images.
There is a lot of information about numpy on the Internet. As the basis of python scientific computing, it is worth studying seriously.
Use image arrays for basic image operations:
Understand image arrays:
Through the following programs we take a look at the image arrays of images and grayscale images, as well as numpy A slice of an array.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Run result:
(600, 500, 3) uint8
64
[64 117 195]
What we see is a three-dimensional array, representing the abscissa, ordinate and color channel respectively.
We can exchange the red and blue channels through the array
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The numpy array slicing method is used here, about There is a lot of information on numpy online, so I won’t go into details.
Run result:
In the process of converting to an array, we can set the data type, and the grayscale image at the same time Arrays are also meaningful:
1 2 3 4 5 6 7 8 9 |
|
Running results:
(600, 500) float32
110.0
The extra parameter 'f' converts the data type of the array to a floating point number
Since the grayscale image has no color information, the shape tuple has only two values.
*The opposite operation of array() transformation can be completed using PIL's fromarray(), such as im = Image.fromarray(im)
Simple application of image array - grayscale transformation:
Grayscale images:
A grayscale digital image is an image with only one sampled color per pixel. Such images typically appear as grayscales ranging from darkest black to lightest white.
You can convert the image to grayscale through the following methods:
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. Only take green: Gray=G;
After obtaining Gray through any of the above methods, replace the R, G, and B in the original RGB (R, G, B) with Gray to form a new color RGB (Gray, Gray, Gray), using It replaces the original RGB (R, G, B) with a grayscale image.
It has been used many times before. Using python, you can obtain the grayscale image by using convert('L')
Grayscale transformation:
After reading the images into NumPy array objects, we can perform arbitrary mathematical operations on them. A simple example is the grayscale transformation of an image. That is, any function f, which maps the 0...255 interval (or the 0...1 interval) to itself.
There are some simple grayscale transformations in the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
Running results:
0 255
0 255
100 200
0 255
You can clearly see the result of the grayscale transformation, No. The two images are displayed in reverse phase. The dark parts of the third image become brighter and the bright parts become darker. The values are limited between 100 and 200. The last image uses a quadratic function to transform the darker pixel values. Darker.
Conclusion:
This blog introduces the process of using image arrays to perform image operations in Python, including several simple examples. Through arrays, we can perform any mathematical operations on images, which is the basis for image deformation, image classification, image clustering, etc. , I hope my blog will be helpful to everyone~
The above is the detailed content of Python image grayscale transformation and image array operation methods. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
