Question
With an image loaded into Python as shown below, how do I know which order the channels are in? (e.g. BGR or RGB)
Code
from PIL import Image
import numpy as np
image_pil = Image.open("Stonehenge.jpg")
image_np = np.array(image_pil)
image_np[0][0]
Result
array([ 52, 123, 155], dtype=uint8)
Specific question
How do I know whether the 52
corresponds to the red channel, the blue channel, or a different channel? Or does this question not make sense on a conceptual level?
Notes
In a similar question for Java instead of Python, one person claims:
If you are reading in the image file, or you have access to the code that reads in the file, know it is:
- BGR order if you used cv2.imread(),
- RGB order if you used mpimg.imread(), (assuming import matplotlib.image as mpimg)
If you don't know how the file was opened, the accepted answer BufferedImage is great for Java.