By reading the image as a NumPy array Show
By operating ndarray, you can get and set (change) pixel values, trim images, concatenate images, etc. Those who are familiar with NumPy can do various image processing without using libraries such as OpenCV. Even when using OpenCV, OpenCV for Python treats image data as This article describes the following contents. Read and write images:
Examples of image processing with NumPy (
Sample codes on this article use Pillow to read and save image files. If you want to use OpenCV, see the following article.
See also the following article about Pillow. Simple operations such as reading, saving, resizing, and rotating images can be done by Pillow alone.
How to read an image file as ndarrayTake the following image as an example. Passing the image data read by
The order of colors (channels) is RGB (red, green, blue). Note that it is different from the case of reading with
If you convert the image to grayscale with
You can also get For
For
The data type If you want to process it as a floating point number
See the
following article for more information about the data type
How to save NumPy array ndarray as image filePassing
A grayscale image (2D array) can also be passed to
If you just want to save it, you can write it in one line.
If the data type
Note that if
the pixel value is represented by With For example, in the case of JPG, you can pass the quality of the image to the argument
Get and set (change) pixel valuesYou can get the value of a pixel by specifying the coordinates at the index
The above
example shows the value at You can also use unpacking to assign them to separate variables.
It is also possible to get the value by specifying the color.
You can also change to a new value. You can change RGB all at once or change it with just a single color.
Generation of single color image and concatenationGenerate single-color images by setting other color values to
Negative-positive inversion (invert pixel value)It is also easy to calculate and manipulate pixel values. A negative-positive inverted image can be generated by subtracting the pixel value from the max value (
Because the original size is too large, it is resized with
Color reductionCut off
the remainder of the division using
BinarizationIt is also possible to assign to black and white according to the threshold. See the following articles for details.
Gamma correctionYou can do anything you want with pixel values, such as multiplication, division, exponentiation, etc. You don't need to use the
As a result of the calculation, the data type Trimming with sliceBy specifying an area with slice, you can trim it to a rectangle.
See the following article for more information on slicing for
It may be convenient to define a function that specifies the upper left coordinates and the width and height of the area to be trimmed.
If you specify outside the size of the image, it will be ignored.
Split with slice or functionYou can also split the image by slicing.
It is also possible to split the image with NumPy function.
If a list is specified as the second argument,
When an integer value is specified as the second argument with
Paste with sliceUsing slices, one array rectangle can be replaced with another array rectangle.
By using this, a part of the image or the entire image can be pasted to another image.
Note that an error will occur if the size of the area specified on the left side differs from the size of the area specified on the right side. Alpha blending and maskingBy the operation for each element (= pixel) of the array, two images can be alpha-blended or composited based on a mask image. See the following articles for details.
Rotate and flipThere are also functions that rotate the array and flip it up, down, left, and right.
Original image: Rotated image: Flipped image: How do I import an image into an array?Converting an Image to Array using NumPy:. from numpy import asarray.. data = asarray(image). print(data). How do you store an image in an array in Python?Using OpenCV Library to Convert images to NumPy array
imread() function is used to load the image and It also reads the given image (PIL image) in the NumPy array format. Then we need to convert the image color from BGR to RGB. imwrite() is used to save the image in the file.
How do I import an image into a matrix in Python?“convert image to matrix python” Code Answer. import matplotlib. image as image.. img=image. imread('image_name.png'). print('The Shape of the image is:',img. shape). print('The image as array is:'). print(img). Which function read an image from a file into an array in Python?Read an image from a file into an array. Use plt. imread() method.
|