7,046 downloads Updated: October 16, 2016 BSD. Description Free Download report malware. A handy, easy to use and open source toolkit that was developed as a facility for sophisticated scientific computing with Python. What's new in NumPy 1.11.2. How to install NumPy for python 3.3.5 on Mac OSX 10.9 (3 answers) Closed 4 years ago. I'm trying to install Numpy on my mac so i can practice doing some programming for data science. There are many alternatives to SciPy & Numpy for Mac if you are looking to replace it. The most popular Mac alternative is R (programming language), which is both free and Open Source.If that doesn't suit you, our users have ranked 39 alternatives to SciPy & Numpy and many of them are available for Mac so hopefully you can find a suitable replacement.
By reading the image as a NumPy array
ndarray
, various image processing can be performed using NumPy functions.By the operation of 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
ndarray
, so it is useful to know how to use NumPy (ndarray
). In addition to OpenCV, there are many libraries such as scikit-image that treat images as ndarray
.This post describes the following contents.
Read and write images:
- How to read image file as NumPy array
ndarray
- How to save NumPy array
ndarray
as image file
Examples of image processing with NumPy (
ndarray
):- Get and set (change) pixel values
- Generation of single color image and concatenation
- Negative / positive inversion (inversion of pixel value)
- Color reduction
- Binarization
- Gamma correction
- Trimming with slice
- Split with slice or function
- Paste with slice
- Alpha blending and masking
- Rotate and flip
Sample codes on this post use Pillow to read and save image files. If you want to use OpenCV, see the following post.
- Related:Reading and saving image files with Python, OpenCV (imread, imwrite)
See also the following post about Pillow. Simple operations such as reading and saving images, resizing and rotating images can be done by Pillow alone.
- Related:How to use Pillow (PIL: Python Imaging Library)
![Numpy Numpy](/uploads/1/1/8/6/118658928/816140896.gif)
How to read an image file as ndarray
Take the following image as an example.
Passing the image data read by
PIL.Image.open()
to np.array()
returns 3D ndarray
whose shape is (row (height), column (width), color (channel))
.The order of colors (channels) is RGB (red, green, blue). Note that it is different from the case of reading with
cv2.imread()
of OpenCV.- Related:Convert BGR and RGB with Python, OpenCV (cvtColor)
If you convert the image to grayscale with
convert('L')
and then pass it to np.array()
, it returns 2D ndarray
whose shape is (row (height), column (width))
.You can also get
ndarray
from PIL.Image
with np.asarray()
. np.array()
returns a rewritable ndarray
, while np.asarray()
returns a non-rewritablendarray
.For
np.array()
, you can change the value of the element (pixel).For
np.asarray()
, you cannot change value because rewriting is prohibited. It is possible to create a new ndarray
based on the read ndarray
.The data type
dtype
of the read ndarray
is uint8
(8-bit unsigned integer).If you want to process it as a floating point number
float
, you can convert it with astype()
or specify the data type in the second argument of np.array()
and np.asarray()
.How to save NumPy array ndarray as image file
Passing
ndarray
to Image.fromarray()
returns PIL.Image
. It can be saved as an image file with save()
method. The format of the saved file is automatically determined from the extension of the path passed in the argument of save()
.A grayscale image (2D array) can also be passed to
Image.fromarray()
. mode
automatically becomes 'L'
(grayscale). It can be saved with save()
.If you just want to save it, you can write it in one line.
If the data type
dtype
of ndarray
is float
etc., an error will occur, so it is necessary to convert to uint8
.Note that if the pixel value is represented by
0.0
to 1.0
, it is necessary to multiply by 255
and convert to uint8
and save.With
save()
, parameters according to the format can be passed as arguments. See Image file format for details.For example, in the case of JPG, you can pass the quality of the image to the argument
quality
. It ranges from 1
(the lowest) to 95
(the highest) and defaults to 75
.- Related:How to use Pillow (PIL: Python Imaging Library)
Get and set (change) pixel values
You can get the value of a pixel by specifying the coordinates at the index
[row, columns]
of ndarray
. Note that the order is y, x
in xy coordinates. The origin is the upper left.The above example shows the value at
(y, x) = (100, 150)
, i.e. the 100th row and 150th column of pixels. As mentioned above, the colors of the ndarray
obtained using Pillow are in RGB order, so the result is (R, G, B) = (111, 81, 109)
.You can also use unpack to assign them to separate variables.
- Related:Unpack a tuple / list in Python
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 you can change it with just a single color.
Generation of single color image and concatenation
Generate single-color images by setting other color values to
0
, and concatenate them horizontally with np.concatenate()
. You can also concatenate images using np.hstack()
or np.c_[]
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 (
255
for uint8
).Because the original size is too large, it is resized with
resize()
for convenience. The same applies to the following examples.- Related:Resize images with Python, Pillow
Color reduction
Cut off the remainder of the division using
//
and multiply again, the pixel values become discrete and the number of colors can be reduced.Binarization
It is also possible to assign to black and white according to the threshold.
See the following articles for details.
- Related:Binarize image with Python, NumPy, OpenCV
Gamma correction
You can do anything you want with pixel values, such as multiplication, division, exponentiation, etc.
You don't need to use the for loop because the entire image can be calculated as it is.
As a result of the calculation, the data type
dtype
of numpy.ndarray
is converted to the floating point number float
. Note that you need to convert it to ʻuint8` when you save it.Trimming with slice
By specifying an area with slice, you can trim it to a rectangle.
See the following post for more information on slicing for
numpy.ndarray
.- Related:NumPy: Slicing ndarray
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 function
You can also split the image by slicing.
It is also possible to split the image with NumPy function.
np.hsplit()
splits ndarray
horizontally. If an integer value is specified for the second argument, ndarray
is splitted equally.If a list is specified as the second argument,
ndarray
is splitted at the position of that values.np.vsplit()
splits ndarray
vertically. The usage of np.vsplit()
is the same as np.hsplit()
.When an integer value is specified as the second argument with
np.hsplit()
or np.vsplit()
, an error will occur if it cannot be splitted equally. np.array_split()
adjusts the size appropriately and splits it.Paste with slice
Using slices, one array rectangle can be replaced with another array rectangle.
- Related:NumPy: Slicing ndarray
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 masking
By 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.
- Related:Alpha blending and masking of images with Python, OpenCV, NumPy
Rotate and flip
There are also functions that rotate the array and flip it up, down, left and right.
- Related:OpenCV, NumPy: Rotate and flip image
Original image:
Install Npm Mac Os
Roteted image:
Numpy Basics For Machine Learning
Flipped image: