- Convert BGR and RGB with Python, OpenCV (cvtColor)
- OpenCV is BGR, Pillow is RGB
- Convert BGR and RGB with OpenCV function cvtColor()
- Convert BGR and RGB without using cvtColor()
- Python image to rgb array
- Convert BGR and RGB with Python, OpenCV (cvtColor)
- OpenCV is BGR, Pillow is RGB
- Convert BGR and RGB with OpenCV function cvtColor()
- Convert BGR and RGB without using cvtColor()
Convert BGR and RGB with Python, OpenCV (cvtColor)
When the image file is read with the OpenCV function imread() , the order of colors is BGR (blue, green, red). On the other hand, in Pillow, the order of colors is assumed to be RGB (red, green, blue).
Therefore, if you want to use both the Pillow function and the OpenCV function, you need to convert BGR and RGB.
You can use the OpenCV function cvtColor() or simply change the order of ndarray .
This article describes the following contents.
- OpenCV is BGR, Pillow is RGB
- Convert BGR and RGB with OpenCV function cvtColor()
- Convert BGR and RGB without using cvtColor()
OpenCV is BGR, Pillow is RGB
When reading a color image file, OpenCV imread() reads as a NumPy array ndarray of row (height) x column (width) x color (3) . The order of color is BGR (blue, green, red).
The OpenCV function imwrite() that saves an image assumes that the order of colors is BGR, so it is saved as a correct image.
import cv2 import numpy as np from PIL import Image im_cv = cv2.imread('data/src/lena.jpg') cv2.imwrite('data/dst/lena_bgr_cv.jpg', im_cv)
When performing image processing with Pillow, you can convert ndarray to a PIL.Image object with Image.fromarray() , but in Pillow the color order assumes RGB (red, green, blue).
Therefore, if the ndarray of the image read by OpenCV imread() is converted to a PIL.Image object and saved, the image with the wrong color is saved.
pil_img = Image.fromarray(im_cv) pil_img.save('data/dst/lena_bgr_pillow.jpg')
If you want to convert ndarray and PIL.Image objects to use both Pillow and OpenCV functions, you need to convert BGR and RGB.
Convert BGR and RGB with OpenCV function cvtColor()
Various color spaces such as RGB, BGR, HSV can be mutually converted using OpenCV function cvtColor() .
Refer to the following document for the value to be specified for the parameter code .
When code is cv2.COLOR_BGR2RGB , BGR is converted to RGB.
When converted to RGB, it will be saved as a correct image even if it is saved after being converted to a PIL.Image object.
im_rgb = cv2.cvtColor(im_cv, cv2.COLOR_BGR2RGB) Image.fromarray(im_rgb).save('data/dst/lena_rgb_pillow.jpg')
When converted to RGB and saved with OpenCV imwrite() , it will be an incorrect color image.
cv2.imwrite('data/dst/lena_rgb_cv.jpg', im_rgb)
The parameter code when converting from RGB to BGR is cv2.COLOR_RGB2BGR . Use this when reading an image file as a PIL.Image , convert it to ndarray , and save it using OpenCV imwrite() .
im_pillow = np.array(Image.open('data/src/lena.jpg')) im_bgr = cv2.cvtColor(im_pillow, cv2.COLOR_RGB2BGR) cv2.imwrite('data/dst/lena_bgr_cv_2.jpg', im_bgr)
Convert BGR and RGB without using cvtColor()
Converting BGR and RGB can be realized without using cvtColor() .
There are several ways, for example, as follows:
im_bgr = cv2.imread('data/src/lena.jpg') im_rgb = im_bgr[:, :, [2, 1, 0]] Image.fromarray(im_rgb).save('data/dst/lena_swap.jpg') im_rgb = im_bgr[:, :, ::-1] Image.fromarray(im_rgb).save('data/dst/lena_swap_2.jpg')
Python image to rgb array
- Numpy | Array Creation
- numpy.arange() in Python
- numpy.zeros() in Python
- Create a Numpy array filled with all ones
- numpy.linspace() in Python
- numpy.eye() in Python
- Creating a one-dimensional NumPy array
- How to create an empty and a full NumPy array?
- Create a Numpy array filled with all zeros | Python
- How to generate 2-D Gaussian array using NumPy?
- How to create a vector in Python using NumPy
- Python | Numpy fromrecords() method
- Copy and View in NumPy Array
- How to Copy NumPy array into another array?
- Appending values at the end of an NumPy array
- How to swap columns of a given NumPy array?
- Insert a new axis within a NumPy array
- numpy.hstack() in Python
- numpy.vstack() in python
- Joining NumPy Array
- Combining a one and a two-dimensional NumPy Array
- Python | Numpy np.ma.concatenate() method
- Python | Numpy dstack() method
- Splitting Arrays in NumPy
- How to compare two NumPy arrays?
- Find the union of two NumPy arrays
- Find unique rows in a NumPy array
- Python | Numpy np.unique() method
- numpy.trim_zeros() in Python
- Matrix manipulation in Python
- numpy matrix operations | empty() function
- numpy matrix operations | zeros() function
- numpy matrix operations | ones() function
- numpy matrix operations | eye() function
- numpy matrix operations | identity() function
- Adding and Subtracting Matrices in Python
- Matrix Multiplication in NumPy
- Numpy ndarray.dot() function | Python
- NumPy | Vector Multiplication
- How to calculate dot product of two vectors in Python?
- Multiplication of two Matrices in Single line using Numpy in Python
- Python | Numpy np.eigvals() method
- How to Calculate the determinant of a matrix using NumPy?
- Python | Numpy matrix.transpose()
- Python | Numpy matrix.var()
- Compute the inverse of a matrix using NumPy
- Reshape NumPy Array
- Python | Numpy matrix.resize()
- Python | Numpy matrix.reshape()
- NumPy Array Shape
- Change the dimension of a NumPy array
- numpy.ndarray.resize() function – Python
- Flatten a Matrix in Python using NumPy
- numpy.moveaxis() function | Python
- numpy.swapaxes() function | Python
- Python | Numpy matrix.swapaxes()
- numpy.vsplit() function | Python
- numpy.hsplit() function | Python
- Numpy MaskedArray.reshape() function | Python
- Python | Numpy matrix.squeeze()
- Random sampling in numpy | ranf() function
- Random sampling in numpy | random() function
- Random sampling in numpy | random_sample() function
- Random sampling in numpy | sample() function
- Random sampling in numpy | random_integers() function
- Random sampling in numpy | randint() function
- numpy.random.choice() in Python
- How to choose elements from the list with different probability using NumPy?
- How to get weighted random choice in Python?
- numpy.random.shuffle() in python
- numpy.random.geometric() in Python
- numpy.random.permutation() in Python
- Numpy | Array Creation
- numpy.arange() in Python
- numpy.zeros() in Python
- Create a Numpy array filled with all ones
- numpy.linspace() in Python
- numpy.eye() in Python
- Creating a one-dimensional NumPy array
- How to create an empty and a full NumPy array?
- Create a Numpy array filled with all zeros | Python
- How to generate 2-D Gaussian array using NumPy?
- How to create a vector in Python using NumPy
- Python | Numpy fromrecords() method
- Copy and View in NumPy Array
- How to Copy NumPy array into another array?
- Appending values at the end of an NumPy array
- How to swap columns of a given NumPy array?
- Insert a new axis within a NumPy array
- numpy.hstack() in Python
- numpy.vstack() in python
- Joining NumPy Array
- Combining a one and a two-dimensional NumPy Array
- Python | Numpy np.ma.concatenate() method
- Python | Numpy dstack() method
- Splitting Arrays in NumPy
- How to compare two NumPy arrays?
- Find the union of two NumPy arrays
- Find unique rows in a NumPy array
- Python | Numpy np.unique() method
- numpy.trim_zeros() in Python
- Matrix manipulation in Python
- numpy matrix operations | empty() function
- numpy matrix operations | zeros() function
- numpy matrix operations | ones() function
- numpy matrix operations | eye() function
- numpy matrix operations | identity() function
- Adding and Subtracting Matrices in Python
- Matrix Multiplication in NumPy
- Numpy ndarray.dot() function | Python
- NumPy | Vector Multiplication
- How to calculate dot product of two vectors in Python?
- Multiplication of two Matrices in Single line using Numpy in Python
- Python | Numpy np.eigvals() method
- How to Calculate the determinant of a matrix using NumPy?
- Python | Numpy matrix.transpose()
- Python | Numpy matrix.var()
- Compute the inverse of a matrix using NumPy
- Reshape NumPy Array
- Python | Numpy matrix.resize()
- Python | Numpy matrix.reshape()
- NumPy Array Shape
- Change the dimension of a NumPy array
- numpy.ndarray.resize() function – Python
- Flatten a Matrix in Python using NumPy
- numpy.moveaxis() function | Python
- numpy.swapaxes() function | Python
- Python | Numpy matrix.swapaxes()
- numpy.vsplit() function | Python
- numpy.hsplit() function | Python
- Numpy MaskedArray.reshape() function | Python
- Python | Numpy matrix.squeeze()
- Random sampling in numpy | ranf() function
- Random sampling in numpy | random() function
- Random sampling in numpy | random_sample() function
- Random sampling in numpy | sample() function
- Random sampling in numpy | random_integers() function
- Random sampling in numpy | randint() function
- numpy.random.choice() in Python
- How to choose elements from the list with different probability using NumPy?
- How to get weighted random choice in Python?
- numpy.random.shuffle() in python
- numpy.random.geometric() in Python
- numpy.random.permutation() in Python
Convert BGR and RGB with Python, OpenCV (cvtColor)
When the image file is read with the OpenCV function imread() , the order of colors is BGR (blue, green, red). On the other hand, in Pillow, the order of colors is assumed to be RGB (red, green, blue).
Therefore, if you want to use both the Pillow function and the OpenCV function, you need to convert BGR and RGB.
You can use the OpenCV function cvtColor() or simply change the order of ndarray .
This article describes the following contents.
- OpenCV is BGR, Pillow is RGB
- Convert BGR and RGB with OpenCV function cvtColor()
- Convert BGR and RGB without using cvtColor()
OpenCV is BGR, Pillow is RGB
When reading a color image file, OpenCV imread() reads as a NumPy array ndarray of row (height) x column (width) x color (3) . The order of color is BGR (blue, green, red).
The OpenCV function imwrite() that saves an image assumes that the order of colors is BGR, so it is saved as a correct image.
import cv2 import numpy as np from PIL import Image im_cv = cv2.imread('data/src/lena.jpg') cv2.imwrite('data/dst/lena_bgr_cv.jpg', im_cv)
When performing image processing with Pillow, you can convert ndarray to a PIL.Image object with Image.fromarray() , but in Pillow the color order assumes RGB (red, green, blue).
Therefore, if the ndarray of the image read by OpenCV imread() is converted to a PIL.Image object and saved, the image with the wrong color is saved.
pil_img = Image.fromarray(im_cv) pil_img.save('data/dst/lena_bgr_pillow.jpg')
If you want to convert ndarray and PIL.Image objects to use both Pillow and OpenCV functions, you need to convert BGR and RGB.
Convert BGR and RGB with OpenCV function cvtColor()
Various color spaces such as RGB, BGR, HSV can be mutually converted using OpenCV function cvtColor() .
Refer to the following document for the value to be specified for the parameter code .
When code is cv2.COLOR_BGR2RGB , BGR is converted to RGB.
When converted to RGB, it will be saved as a correct image even if it is saved after being converted to a PIL.Image object.
im_rgb = cv2.cvtColor(im_cv, cv2.COLOR_BGR2RGB) Image.fromarray(im_rgb).save('data/dst/lena_rgb_pillow.jpg')
When converted to RGB and saved with OpenCV imwrite() , it will be an incorrect color image.
cv2.imwrite('data/dst/lena_rgb_cv.jpg', im_rgb)
The parameter code when converting from RGB to BGR is cv2.COLOR_RGB2BGR . Use this when reading an image file as a PIL.Image , convert it to ndarray , and save it using OpenCV imwrite() .
im_pillow = np.array(Image.open('data/src/lena.jpg')) im_bgr = cv2.cvtColor(im_pillow, cv2.COLOR_RGB2BGR) cv2.imwrite('data/dst/lena_bgr_cv_2.jpg', im_bgr)
Convert BGR and RGB without using cvtColor()
Converting BGR and RGB can be realized without using cvtColor() .
There are several ways, for example, as follows:
im_bgr = cv2.imread('data/src/lena.jpg') im_rgb = im_bgr[:, :, [2, 1, 0]] Image.fromarray(im_rgb).save('data/dst/lena_swap.jpg') im_rgb = im_bgr[:, :, ::-1] Image.fromarray(im_rgb).save('data/dst/lena_swap_2.jpg')